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

How do you find and hyperlink all URLs from text using a regular expression?

到目前为止,这是我的函数并且运行良好,但是它开始使用逗号超链接文本,后跟一行 return:

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

例如:

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

I was able to find everything.");

将return这个:

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>

谁能帮我弄清楚我做错了什么?

这会略微提高原始模式的准确性。我的模式运行速度几乎是您模式的两倍。我已经删除了 unwanted/unused 捕获组,改进了关于可选 // 的模式准确性,在模式末尾添加了一个不区分大小写的标志,删除了不必要的转义,并且为了简洁起见基本上压缩了你的模式。

Pattern Demo With Replacement

代码:(Demo)

function linkify($text){
    $capture='@(?:http(s)?://)?([a-z][-\w]+(?:\.\w+)+(?:\S+)?)@i';
    $replace='<a href="http://" target="_blank" title="[=10=]">[=10=]</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."),"\n\n---\n\n";
echo linkify("http://google.com"),"\n\n---\n\n";
echo linkify("http://google.com.au"),"\n\n---\n\n";
echo linkify("https://google.com.au"),"\n\n---\n\n";
echo linkify("www.google.com"),"\n\n---\n\n";
echo linkify("google.com"),"\n\n---\n\n";
echo linkify("I went to the local food store and bought some food.\n\nI was able to find everything"),"\n\n---\n\n";
echo linkify("I went to the local food store and bought some food.

I was able to find everything");

输出:

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

这可能不是所有可能网址的灵丹妙药,但它是一个合理的基础。如果您发现某些字符串没有按预期替换,则可能需要对模式进行一些调整。


模式 update/extension 以包含带有子域的 url:

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

我只是在字符 class 上加了一个点。