/ Published in: PHP
Many developers has trouble when they have to check for urls inside a large textual string. This solution will help you to change the url into html link tags even when the user only inserts a url like 'www.mysite.com'
Expand |
Embed | Plain Text
<?php // this function shall search for any url that matches the pattern // like: // www.mysite.com // http://www.mysite.com // http://mysite.com // those urls shall be replaced by a html link tag $text= preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1" target="blank">$1</a>', $text); //because you want the url to be an external link the href needs to start with 'http://' //simply replace any occurance of 'href="www.' into 'href="http://www."
You need to login to post a comment.
