danrevah — 2011-11-09T06:42:46-05:00 — #1
i wrote this code and i was having a problem since
it will convert this:
<a target="_blank" rel="nofollow" href="http://www.ccc.com">http://www.ccc.com</a>
but if there was already a link:
it will change it like that:
<a href="http://www.xxx.com<a href="http://www.xxx.com">http://www.xxx.com"></a></a>
how can i make my code ignore the links?
return ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a target=\\"_blank\\" rel=\\"nofollow\\" href=\\"/go/\\\\0\\">\\\\0</a>", $str);
starlion — 2011-11-09T08:38:15-05:00 — #2
preg (PS: EREG is deprecated.) does not have the logic built in to handle this in one line without some really awful and long patterns.
Incidentally, your system will fail to pass variables, because & and ? are not in your parsing.
Simple suggestion would be to replace all current links with a temporary value (Something like #http;//theurl# , though you should probably use a more obscure character than that. Note the semicolon instead of the colon), then do your replacement, then undo the temporary values.