note 69185 added to function.preg-replace

From: Date: Fri, 25 Aug 2006 20:52:04 +0000
Subject: note 69185 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-116331@lists.php.net to get a copy of this message
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; } ---- Server IP: 216.194.113.175 Probable Submitter: 204.254.175.249 (proxied: 10.157.25.71) ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/69185 Del: integrated -- https://master.php.net/note/delete/69185/integrated Del: useless -- https://master.php.net/note/delete/69185/useless Del: bad code -- https://master.php.net/note/delete/69185/bad+code Del: spam -- https://master.php.net/note/delete/69185/spam Del: non-english -- https://master.php.net/note/delete/69185/non-english Del: in docs -- https://master.php.net/note/delete/69185/in+docs Del: other reasons-- https://master.php.net/note/delete/69185 Reject -- https://master.php.net/note/reject/69185 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116331) next »