0

I've created a message system and would like to auto-convert url-links in a message to clickable HTML links if a new message is posted. I wrote this simple function but it isn't working as it should:

    // LINK ALL URLS
  $message = ereg_replace("http://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $message);
  $message = ereg_replace("(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message);

For some urls it is working, but with other urls there are problems and the results are like this:

<a href="http://www.example.com/index.php">http://www.example.com/index.php</a>?mode=index&page=1

or

<a href="http://www.youtube.com/watch">http://www.youtube.com/watch</a>?v=jSh5Y7jq9FQ

As you can see, it is not correctly converted inclusive the part behind the question mark. Can someone please fix / update my code above? And by the way, would there be perhaps another (and better!) solution instead of using *ereg_replace* ?

  • 5
    This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. Workaround: Use `preg_replace()` – hek2mgl Jun 17 '13 at 12:25
  • Thank you for this info, I knew about that is has been DEPRECATED, for now I was just looking for a quick fix, in future I will use preg_replace(). –  Jun 17 '13 at 12:38
  • 2
    @netsetter - honestly, it's *very* easy to convert ereg to preg. 99% of cases it just involves changing the function name and adding delimiter characters to the ends of the string. You really should do it. Also, it's worth adding that not only is it deprecated in 5.3, it has been removed entirely from 5.4. It's a *lot* easier to change it while you're still running on a version that supports both (that's why the do the deprecation process), so I strongly recommend changing it sooner rather than later. – Spudley Jun 17 '13 at 12:41
  • @Spudley: Alright, if you say that it is _very easy_ to convert, I will take the time now to update the code. Thanks. –  Jun 17 '13 at 12:45

3 Answers3

2
 $message = preg_replace('#((?:[a-zA-Z]+://|www)[^ ]+)#i', '<a href="$0">$0</a>', $message);

This might help you

jamek
  • 792
  • 10
  • 20
  • even more professional and shorter than my code, and it's working –  Jun 17 '13 at 13:14
  • Oh, wait, it's working but only if just **one link** is in the message, but with more than 1 link it doesn't work correctly anymore, do you have a solution for this? –  Jun 17 '13 at 13:21
2

This is the solution I'm using now which seems to work correctly, inclusive the question mark fix and the suggestions in the comments to convert ereg_replace() to preg_replace():

// LINK ALL URLS
      $message = preg_replace("#http://([.]?[a-zA-Z0-9_/-?])*#", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $message);
      $message = preg_replace("#(^| |\n)(www([.]?[a-zA-Z0-9_/-?])*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message);
1

Your regex does not allow the ? character, so of course the link cuts off before any query string. put ? in your character class. While you're at it you also need to allow every single other valid URL character.

Consult this question and its answers for an idea of what makes a valid URL regex.

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • You're right, putted the question mark into the allowed characters part and it's working now. Thanks –  Jun 17 '13 at 12:43