Regex to replace tags in a string in PHP -
i using php , regex below search through huge string
[link]www.anyurl.com[/link]
and replace with:
<a href="http://www.anyurl.com">http://www.anyurl.com</a>
the http:// prepended if http or ftp not exist in front of url already.
$re = '/\[link]((?:(?!(http|ftp)|\[\/link]).)*)\[\/link]/i'; $subst = '[link]http://$1[/link]'; $xtext = preg_replace($re, $subst, $xtext, 1); $xtext = preg_replace("/(\[link\])(\s*)(\[\/link])/", "<a href=\"$2\" target='_blank'>$2</a>", $xtext);
my problem appears working first match comes , not other [link]www.urls.com[/link]
in document. document being $xtext
whoops, have found error , simple. in third line:
$xtext = preg_replace($re, $subst, $xtext, 1);
i have 1 @ end of preg_replace i.e. replace once. should set -1 or left blank replace all.
Comments
Post a Comment