note 69185 deleted from function.preg-replace by bjori
| From: | bjori@php.net | Date: | Sat, 26 Aug 2006 11:17:27 +0000 |
| Subject: | note 69185 deleted from function.preg-replace by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-116342@lists.php.net to get a copy of this message | ||
Note Submitter: Paul N
Reason: in docs
----
I needed a way to display a user-entered text field so that links and emails that they may have
embedded in it would be displayed as HTML hyperlinks. Here's the function I came-up with...
hope you find it useful.
Paul N (savage_25 at yahoo.com)
function MagicLinks ($str) {
// Takes any input string and locates email addresses and web addresses within it, which are then
turned into hyperlinks using HTML.
$rtnStr = ' ' . $str . ' '; // add whitespace at start and end to handle links
at beginning and/or end
// find websites (with protocol) "http[s]:*"
$rtnStr = preg_replace('/([\s\n])(https*:\S+)([\s$])/i','$1<a href="$2"
target="_blank">$2</a>$3',$rtnStr);
// find websites (without protocol, but have www) "www.*"
$rtnStr = preg_replace('/([\s\n])(www\.\S+)([\s$])/i','$1<a href="http://$2"
target="_blank">$2</a>$3',$rtnStr);
// find emails: "*@*"
$rtnStr = preg_replace('/([\s\n])([_a-z0-9-]+@[_a-z0-9-.]+)([\s$])/i','$1<a
href="mailto:$2">$2</a>$3',$rtnStr);
$rtnStr = substr($rtnStr,1,strlen($rtnStr)-2); // remove added whitespace at start and end
return $rtnStr;
}