Reputation: 194
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.<br" target="_blank" title="seed.<br">food.<br< a=""> />
<br>
I was able to find everything.</br<></a>
Can someone please help me figure out what I'm doing wrong here?
Upvotes: 1
Views: 319
Reputation: 48100
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.
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."),"\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");
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.
Upvotes: 2