down00111 2014-12-14 19:35
浏览 33
已采纳

使用php在正则表达式中查找baseDomain

What i wanted to do is to search for $BaseDomain if it exist within the link match by regex and if true no need of opening the link in another tap. I also tried using $replacePattern1 instead of $1 and $replacePattern2 instead of $2. The target = _blank is never appended to the href when the the link is not the $BaseDomain. Please where am i wrong?

function ReplaceUrlToHtmlLink($source) {
           /// my site name ///
            $BaseDomain = "ivotism.com";
    //URLs starting with http://, https://, or ftp://
    $replacePattern1 = '/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i';
    $source =  preg_replace($replacePattern1, '<a href="$1" ' . (strpos('$1', $BaseDomain) !== false ?
                    ' target="_blank"' : '') . '>$1</a>', $source);

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    $replacePattern2 = '/(^|[^\/])(www\.[\S]+(\b|$))/i';
    $source =  preg_replace($replacePattern2, ' <a href="http://$2" ' . (strpos('$2', $BaseDomain) !== false ?
                    ' target="_blank"' : '') .'>$2</a>', $source);

    //Change email addresses to mailto:: links.
    $replacePattern3 = '/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/i';
    $source =  preg_replace($replacePattern3, '<a href="mailto:$1">$1</a>', $source);
     return $source;
}
  • 写回答

2条回答 默认 最新

  • drhzc64482 2014-12-14 19:49
    关注

    Use === true in all your tests. Because !== false work also for 0.

    But it is not possible to use $1 (or $n) in other function, even in the same line argument.

    Use:

    function ReplaceUrlToHtmlLink($source) {
        /// my site name ///
            $BaseDomain = "ivotism.com";    // or "ivotism\.com"
    
        //URLs starting with http://, https://, or ftp://
        $replacePattern1 = '/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i';
        $source =  preg_replace($replacePattern1, '<a href="$1" target="_blank">$1</a>', $source);
    
        //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
        $replacePattern2 = '/(^|[^\/])(www\.[\S]+(\b|$))/i';
        $source =  preg_replace($replacePattern2, ' <a href="http://$2" target="_blank">http://$2</a>', $source);
    
        //Change link with domain.
        $replacePattern3 = '/(<a href="[^"]+'. $BaseDomain .'[^"]*") target="_blank">/i';
        $source =  preg_replace($replacePattern3, '$1>', $source);
    
        //Change email addresses to mailto:: links.
        $replacePattern4 = '/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/i';
        $source =  preg_replace($replacePattern4, '<a href="mailto:$1">$1</a>', $source);
         return $source;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程