PHP正则表达式替换链接url

时间:2010-10-27 19:31:15

标签: php regex

我需要在 href=" 之前添加 http:// ,如果此 http:// 不关注 href=" src="

以下代码部分有效。部分意味着它仅考虑 <a href=" ,但不考虑 src="

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

先谢谢你们的回复。

1 个答案:

答案 0 :(得分:7)

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