Preg 将文本替换为带有域筛选器的链接


Preg Replace text to links with domains filter

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

$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);
}

编辑:这对我有帮助:

$text =  preg_replace("~((?:http|https|ftp)://(?!site.com|site2.com|site3.com)(?:'S*?'.'S*?))(?='s|';|')|']|'[|'{|'}|,|'"|'|:|'<|$|'.'s)~i",'<a href="$1" target="_blank">$1</a>',$text);  
对于

此类情况,您可以使用(?!...)否定的前瞻性断言。只需在协议占位符 :// 之后立即添加(?!somelink3.org|somelink2.com)

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

此外,您不应该将preg_match_all与笨拙的str_replace结合使用作为次要步骤。而是利用preg_replace_callback并将所有逻辑放在一个函数中。

您可能可以将其压缩并使用预件替换所有

原始正则表达式

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

原始替换

<a href="http://$1" target="_blank">$1</a>

修饰符//xsg

编辑: - 所以我没有发现你需要过滤域。上面的正则表达式过滤 jpg/png/gif 文件,这相当复杂。但是添加过滤器可能最好使用 url 解析器或回调中的其他正则表达式来处理。