Regex NOT IN single or double quotes
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>
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}....(?!["'])/', ...)
精彩评论