Recently I was developing a custom solution for one of my client’s projects and I encountered a problem: A message (string) was stored in the database which may contain urls. I had to display that message and if it contains urls – it had to be display as active, clickable links. So here’s what worked for me:

PHP Snippet: Find URLs in string and make links using a function

$yourTextWithLinks = 'Here is an example for a text (string) that contains one or more url. Just visit http://www.google.com/ or <a href="http://www.google.com" rel="nofollow">http://google.com</a> and this is the end of the example.';
$text = strip_tags($yourTextWithLinks);

function displayTextWithLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1">$1</a>', $s);
}

$text = displayTextWithLinks($text);
echo $text;

Normally if you echo the variable $yourTextWithLinks it will display plain text with no active links. So now you can pass your string to the displayTextWithLinks() function and will return a text with clickable links in it.

If you’d like to use it without a function, here’s how.

PHP Snippet: Find URLs in text and make links without a function

$text = strip_tags($yourTextWithLinks);
$textWithLinks = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank" rel="nofollow">$1</a>', $text);
echo $textWithLinks;

Learn more about:


Our reader Eugen found that the above solution is not working with urls containing non latin characters and found the following library:

Url highlight

Url highlight is a PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input.

Url highlight features:

  • Replace URLs in string by HTML tags (make clickable)
  • Match URLs without scheme by top-level domain
  • Work with HTML entities encoded input
  • Extract URLs from string
  • Check if string is URL

You can find the library in GitHub.




Recent Articles

Blog Categories

Related Articles

If you enjoyed reading this, then please explore our other articles below:

Comments

Leave a comment
  1. Brian August 25, 2020 at 2:01 am - Reply

    A problem I had with this is if you are striping the tags out of it first, you can’t preserve any unique identifiers given to the link like: Check out this new search engine!

    The following will preserve those tags as well as account for any encapsulating punctuation:

    $txt = “Test text http://hello.world Test text
    http://google.com/file.jpg Test text https://hell.o.wor.ld/test?qwe=qwe Test text
    test text http://test.test/test I am already linked up
    It was also done in 1927 (http://test.com/reference) Also check this out:http://test/index&t=27“;

    $holder = explode(“http”,$txt);
    for($i = 1; $i http$href“,explode($href,$holder[$i]));
    $holder[$i-1] .= “http://hello.world Test text
    http://google.com/file.jpg Test text https://hell.o.wor.ld/test?qwe=qwe Test text
    test text http://test.test/test I am already linked up
    It was also done in 1927 (ht

  2. Jim August 8, 2020 at 9:37 am - Reply

    The aforementioned highlight library will not install on PHP version 7.0.33.0. I opened an issue at github.

  3. Eugen May 19, 2020 at 10:09 pm - Reply

    This solution is limited to http(s) protocol and not working with urls containing non latin characters. Found a library to deal with this cases: https://github.com/vstelmakh/url-highlight

    • webroom May 20, 2020 at 7:45 am - Reply

      Great! Thanks for contributing to the topic! I’ve added it in the article as alternative solution.

Leave a Reply

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.