Preg_replace URLs and images in PHP
I have some string with text and I want to replace all image links to img tags and other links to a tags.
I already 开发者_StackOverflow社区did that:
$row['message'] = preg_replace('/(http:\\/\\/.+(png|jpeg|jpg|gif|bmp))/Ui', '<img height="'.self::getConfig('image_height').'" src="$1" />', $row['message'], -1, $countImages);
How to replace all links except in img tags to a tag in $row['message']
?
Thanx!
Add a negative lookbehind to your regex to see if the URL follows src="
.
$row['message'] = preg_replace('/(?<!src=")(http:\\/\\/.+(png|jpeg|jpg|gif|bmp))/Ui', '<img height="'.self::getConfig('image_height').'" src="$1" />', $row['message'], -1, $countImages);
精彩评论