0

I'm using following regex to find and replace any external url in my content and convert it to active link.

$content = preg_replace('#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '\\1<a href="/_goto.php?exturl=http://\\2" target="_blank" class="external" rel="nofollow">\\2</a>', $content);
$content = preg_replace('/<a(.*?)>www./', '<a$1>', $content);

It works fine but don't catch urls between <li> or other tags.

Ex: <li>www.google.com</li>

How can I modify it to work in that cases? Thanks

  • This post might help you: http://stackoverflow.com/questions/4217406/php-add-link-to-a-url-in-a-string – Peon Dec 04 '13 at 09:58
  • Your patterns look rather complicated for this task. Is it a requirement that external URLs must begin on the first character of a line and thus something like `$content = 'My http://google.com'` would not be converted? – svoop Dec 04 '13 at 09:58
  • There are lots of examples on the net. Can you narrow down the type of url's you'll want to match as there are so many combinations it's mind blowing. Is there a format you can guarantee – gwillie Dec 04 '13 at 10:03

1 Answers1

0

In the first statement, change preg_replace('#(^|[\n ]) to preg_replace('#(^|[\n >])

But you have another problem. Although your regex will capture a fragment identifier at the end of a url (#like_this), it will be appended directly to the redirect URL and not to the URL you are trying to redirect to. You'll have to use preg_replace_callback() to fix this.

And there are lots of other cases you're missing, like URLs with a trailing full stop, comma, question mark or closing bracket that needs to be removed. Or URLs preceded by an opening bracket or tab... et cetera...

r3mainer
  • 23,981
  • 3
  • 51
  • 88