note 78802 added to function.preg-replace

From: Date: Sun, 28 Oct 2007 22:38:40 +0000
Subject: note 78802 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-131796@lists.php.net to get a copy of this message
@ Mac: Your adjustment only seems to work when the period or comma is followed by a newline. The code breaks when more characters follow. Try the following code instead. It parses the url to a point where all punctuation is followed by either a word character or a slash. <?php $string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2">\\2</a>', $string); $string = preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is', '\\1<a href="mailto:\\2">\\2</a>', $string); ?> This seems fairly waterproof but please post your improvements. ---- Server IP: 85.12.26.134 Probable Submitter: 85.144.41.234 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/78802 Del: integrated -- https://master.php.net/note/delete/78802/integrated Del: useless -- https://master.php.net/note/delete/78802/useless Del: bad code -- https://master.php.net/note/delete/78802/bad+code Del: spam -- https://master.php.net/note/delete/78802/spam Del: non-english -- https://master.php.net/note/delete/78802/non-english Del: in docs -- https://master.php.net/note/delete/78802/in+docs Del: other reasons-- https://master.php.net/note/delete/78802 Reject -- https://master.php.net/note/reject/78802 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131796) next »