dtntjwkl83750 2014-12-14 13:28
浏览 40
已采纳

使用php在函数中使用多个正则表达式

I wanted to pass data that contains url within it, and the url will be converted to click-able links, but the issue is that when i run the function, it reiterate 3 times and the data is treated 3 times. How can I have a single data output? I tried removing the concatenation .= to just = but then, only the last pattern is being treated. I want it in 3 pattern because I want to add http to the href when a user input is www.ivotism.com, it will be <a href="http//www.ivotism.com">www.ivotism.com</a> instead of <a href="www.ivotism.com">www.ivotism.com</a>.

function linkify($inputText) {
        //URLs starting with http://, https://, or ftp://
        $replacePattern1 = '/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i';
        $replacedText .=  preg_replace($replacePattern1, '<a href="$1" target="_blank">$1</a>', $inputText);

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

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

        return $replacedText;
    }

I run the code as shown below:

$ab = "<br>1 http://www.ivotism.com <br>2 https://www.ivotism.com/hom.php?u=kira&id=2 <br>3 ftp://www.ivotism.com <br>4 w www.ivotism.com <br>5 info@ivotism.com"; echo 
linkify($ab);

This is the result I got:

  1. 1 http://www.ivotism.com
  2. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  3. 3 ftp://www.ivotism.com
  4. 4 www.ivotism.com
  5. 5 info@ivotism.com
  6. 1 http://www.ivotism.com
  7. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  8. 3 ftp://www.ivotism.com
  9. 4 www.ivotism.com
  10. 5 info@ivotism.com
  11. 1 http://www.ivotism.com
  12. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  13. 3 ftp://www.ivotism.com
  14. 4 www.ivotism.com
  15. 5 info@ivotism.com
  • 写回答

1条回答 默认 最新

  • douwo3665 2014-12-14 13:46
    关注

    You can perform all the replacements in a single call to preg_replace, by putting the regular expressions and replacements into arrays.

    $resultText = preg_replace(array($replacePattern1, $replacePattern2, $replacePattern3),
                               array($replaceSub1, $replaceSub2, $replaceSub3),
                               $inputText);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog