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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -