ereg_replace not working correctly

173 Views Asked by At

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* ?

3

There are 3 best solutions below

1
Niet the Dark Absol On BEST ANSWER

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.

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

This might help you

0
AudioBubble On

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);