[b]code[/b]
$myString="I often visit <url>www.sitepoint.com/forums</url> and love it.";
$myString=preg_replace('/<url>(.*?)<\\/url>/s',
'<a href="http://$1">http://$1</a>',
$myString);
echo $myString;
[b]result[/b]
I often visit
<a href="http://[COLOR="blue"]www.sitepoint.com/forums[/COLOR]">[COLOR="Red"]http://www.sitepoint.com/forums[/COLOR]</a>
and love it.
The code above works fine with the result above.
I like to make it like the following.
if there is any space between <url> and </url>, the string which is after the first space will be the link title.
The would-be code and my target result will be like the following.
[b]would-be code[/b]
$myString="I often visit
<url>[COLOR="blue"]www.sitepoint.com/forum[SIZE="7"]s[/SIZE][/COLOR] [COLOR="Red"][SIZE="7"]s[/SIZE]itepointForums[/COLOR]</url>
and love it.";
$myString=preg_replace('/<url>(.*?)<\\/url>/s',
'<a href="http://$1">[COLOR="Red"]$afterSpace[/COLOR]</a>', $myString);
[b]target result[/b]
I often visit
<a href="http://[COLOR="Blue"]www.sitepoint.com/forums[/COLOR]">[COLOR="red"]sitepointForums[/COLOR]</a>
and love it.