开发者

PHP Regular expression to replace link url

开发者 https://www.devze.com 2023-01-22 10:39 出处:网络
I need to add href=\" before a开发者_C百科n http:// if this http:// doesn\'t follow href=\" or src=\"

I need to add href=" before a开发者_C百科n http:// if this http:// doesn't follow href=" or src="

Following code partly works. Partly means it considers <a href=" only but not src="

$str= preg_replace( 
    "/(?<!a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", 
    "<a href=\"\\0\"> target=\"blank\">\\0</a>", 
    $str
);

Thank you guys in advance for your reply.


$str= preg_replace( 
    "/(?<!a href=\")(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", 
    "<a href=\"\\0\" target=\"blank\">\\0</a>", 
    $str
);
0

精彩评论

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