Php Preg用域过滤器替换链接的文本

Php Preg用域过滤器替换链接的文本,php,regex,Php,Regex,大家好我有个问题 我有一条短信 $text = " some and text http://www.somelink2.com/html5.jpg and some text http://www.somelink.net/test/testjava/html5.html#fbid=QTb-X6fv5p1 some http://www.somelink4.org test and http://www.somelink3.org/link.html text and some text ";

大家好我有个问题 我有一条短信

$text = " some and text http://www.somelink2.com/html5.jpg and some text http://www.somelink.net/test/testjava/html5.html#fbid=QTb-X6fv5p1 some http://www.somelink4.org test and http://www.somelink3.org/link.html text and some text ";
我需要转换所有文本链接http/s exept域somelink3.org,somelink2.com它们必须是纯文本

类似于此,但带有域过滤器而不是扩展图像:

function livelinked ($text){
        preg_match_all("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)|^(jpg)^#ie", $text, $ccs);
        foreach ($ccs[3] as $cc) {
           if (strpos($cc,"jpg")==false  && strpos($cc,"gif")==false && strpos($cc,"png")==false ) {
              $old[] = "http://".$cc;
              $new[] = '<a href="http://'.$cc.'" target="_blank">'.$cc.'</a>';
           }
        }
        return str_replace($old,$new,$text);
}
函数livelinked($text){
preg|u match|u all(“|((http | https | ftp):/(\S*?\。\S*?)(\S | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

 #((http|https|ftp)://(?!domain1|domain2)(\S*?\.\S*?))....

另外,您不应该将
preg\u match\u all
与笨拙的
str\u replace
结合使用作为第二步。相反,您可以将所有逻辑使用并放在一个函数中。

您可能可以将其压缩并使用preg replace all

原始正则表达式

(?:http|https|ftp)://
(\S*?\.(?:(?!(?<=\.)(?:jpg|png|gif)|\s).)*?)
(?= [\s;)\]\[{},"':<] | $ | \.\s )
(?:http | https | ftp)://

(\S*?\。(?:(?!)非常感谢,iv gor a成功,我有一些问题:为什么使用preg match不好?当我发布两个链接时,我得到了
www.domain.com/test ajax upload.html“target=“\u blank”>www.domain.com/test-ajax-upload.html www.domain.com/test ajax upload.html”target=“\u blank”>www.domain.com/test ajax upload.html
(1)因为它很笨拙。(2)因为您还匹配了
\s
空格字符。
(?:http|https|ftp)://
(\S*?\.(?:(?!(?<=\.)(?:jpg|png|gif)|\s).)*?)
(?= [\s;)\]\[{},"':<] | $ | \.\s )
<a href="http://$1" target="_blank">$1</a>