I can say blue parts are urls and red parts are link texts.Code:$myString1="I often visit <url>www.sitepoint.com/forums</url> and <url>www.google.com</url>."; $myString2="I often visit <url>www.sitepoint.com/forums sitePointForums</url> and <url>www.google.com google</url>."; $myString1=preg_replace('/<url>(.*?)<\/url>/s', '<a href="http://$1">$1</a>', $myString1); $myString2=preg_replace('/<url>(.*?) (.*?)<\/url>/s', '<a href="http://$1">$2</a>', $myString2); echo $myString1.'<hr>'; echo $myString2.'<hr>'; result I often visit <a href="http://www.sitepoint.com/forums">www.sitepoint.com/forums</a> and <a href="http://www.google.com">www.google.com</a><hr> I often visit <a href="http://www.sitepoint.com/forums">sitePointForums</a> and <a href="http://www.google.com">google</a><hr>
In myString2, there are spaces as delimiters between www.sitepoint.com/forums and sitePointForums or www.google.com and google which are inside the start <url> tag and the end </url> tag.
On the contrary, myString1 has no space between the start <url> tags and the end </url> tags.
In myString1, urls become directly link texts.
If I have myString3 which has links with delimiters and without delimiters like the below,
how can I make my target result below when a custom tag of <url> has space as a delimiter make the link text with the text after delimiter
and when a custom tag of <url> has no space make the link text with the url itself.
Code:$myString3="I often visit <url>www.sitepoint.com/forums</url> and <url>www.google.com google</url>."; result I often visit <a href="http://www.sitepoint.com/forums">www.sitepoint.com/forums</a> and <a href="http://www.google.com">google</a><hr>



Reply With Quote

Bookmarks