douzhuolong9886 2017-07-19 00:11
浏览 266
已采纳

如何使用正则表达式从文本中查找和超链接所有URL?

Here is my function so far and it works pretty well, however it starts hyper-linking text with a comma followed by a line return:

function linkify($text) {
    $url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^‌,.\s])@';
    $string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $text);
    return $string;
}

For example:

echo linkify("I went to the local food store and bought some food.

I was able to find everything.");

Will return this:

I went to the local food store and bought some <a href="http://seed.&lt;br" target="_blank" title="seed.<br">food.<br< a=""> /&gt;

<br>
I was able to find everything.</br<></a>

Can someone please help me figure out what I'm doing wrong here?

  • 写回答

1条回答 默认 最新

  • doudou2121 2017-07-19 01:04
    关注

    This will slightly improve the accuracy of your original pattern. My pattern will operate nearly twice as a fast as your pattern. I have removed unwanted/unused capture groups, improved pattern accuracy regarding optional //, added a case-insensitive flag to the end of the pattern, removed needless escapes, and basically condensed your pattern for the sake of brevity.

    Pattern Demo With Replacement

    Code: (Demo)

    function linkify($text){
        $capture='@(?:http(s)?://)?([a-z][-\w]+(?:\.\w+)+(?:\S+)?)@i';
        $replace='<a href="http$1://$2" target="_blank" title="$0">$0</a>';
        $string = preg_replace($capture,$replace,$text);
        return $string;
    }
    
    echo linkify("Here is a sentence with a url containing a query string: https://www.google.com/search?q=mickmackusa&oq=mickmackusa&aqs=chrome..69i57j69i60.271j0j7&sourceid=chrome&ie=UTF-8 all good."),"
    
    ---
    
    ";
    echo linkify("http://google.com"),"
    
    ---
    
    ";
    echo linkify("http://google.com.au"),"
    
    ---
    
    ";
    echo linkify("https://google.com.au"),"
    
    ---
    
    ";
    echo linkify("www.google.com"),"
    
    ---
    
    ";
    echo linkify("google.com"),"
    
    ---
    
    ";
    echo linkify("I went to the local food store and bought some food.
    
    I was able to find everything"),"
    
    ---
    
    ";
    echo linkify("I went to the local food store and bought some food.
    
    I was able to find everything");
    

    Output:

    Here is a sentence with a url containing a query string: <a href="https://www.google.com/search?q=mickmackusa&oq=mickmackusa&aqs=chrome..69i57j69i60.271j0j7&sourceid=chrome&ie=UTF-8" target="_blank" title="https://www.google.com/search?q=mickmackusa&oq=mickmackusa&aqs=chrome..69i57j69i60.271j0j7&sourceid=chrome&ie=UTF-8">https://www.google.com/search?q=mickmackusa&oq=mickmackusa&aqs=chrome..69i57j69i60.271j0j7&sourceid=chrome&ie=UTF-8</a> all good.
    
    ---
    
    <a href="http://google.com" target="_blank" title="http://google.com">http://google.com</a>
    
    ---
    
    <a href="http://google.com.au" target="_blank" title="http://google.com.au">http://google.com.au</a>
    
    ---
    
    <a href="https://google.com.au" target="_blank" title="https://google.com.au">https://google.com.au</a>
    
    ---
    
    <a href="http://www.google.com" target="_blank" title="www.google.com">www.google.com</a>
    
    ---
    
    <a href="http://google.com" target="_blank" title="google.com">google.com</a>
    
    ---
    
    I went to the local food store and bought some food.
    
    I was able to find everything
    
    ---
    
    I went to the local food store and bought some food.
    
    I was able to find everything
    

    This may not be the silver bullet for all possible urls, but it is a reasonable foundation. If you find that some strings aren't replacing as expected, the pattern may need some tweaking.


    Pattern update/extension to include urls with subdomains:

    ~(?:ht{2}p(s)?:/{2})?([a-z][-\w.]+(?:\.\w+)+(?:\S+)?)~i
    //  new dot here---------------^
    

    I merely added a dot to the character class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥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,如何解決?