开发者

Preg_replace URLs and images in PHP

开发者 https://www.devze.com 2023-03-26 20:48 出处:网络
I have some string with text and I want to replace all image links to img tags and other links to a tags.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消