0

I've been using this Regex to convert single text links into Html links. It works fine for plain text, but whenever someone inserts a proper html link or an iframe or anything that uses a http:// preceeded text, it makes it a link. To explain:

preg_replace('/([hf][tps]{2,4}:\/\/[^ \t\n\r]+[^ .\t,\n\r\(\)"\'])/', '<a href="$1">$1</a>', $string)

makes the job, but it ruins:

<a href='...'>...

, <iframe src='...'>... and every other a mess.

I've been trying to use:

^[^'"]*([hf][tps]{2,4}:\/\/[^ \t\n\r]+[^ .\t,\n\r\(\)"\'])$

But it makes:

juanito mario http://...

become

<a href="juanito mario http://...">juanito mario http://...</a>
hakre
  • 193,403
  • 52
  • 435
  • 836
edoher
  • 61
  • 1
  • 6

1 Answers1

0

Use negative lookbehind and lookahead assertion, see

http://www.php.net/manual/en/regexp.reference.assertions.php

cite: (?<!foo)bar does find an occurrence of "bar" that is not preceded by "foo". foo(?!bar) matches any occurrence of "foo" that is not followed by "bar".

So in your case something like

preg_replace('/(?<!["'])([hf][tps]{2,4}....(?!["'])/', ...)
Tomas
  • 57,621
  • 49
  • 238
  • 373