note 88265 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Mon, 19 Jan 2009 16:50:59 +0000 |
| Subject: | note 88265 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149451@lists.php.net to get a copy of this message | ||
Note Submitter: lassial at iki dot fi
----
In attempt to automatically convert URLs to hyperlinks I adopted the solution offered by tal at
ashkenazi dot co dot il
However, I ran into problem of tackling previously insert HTML line breaks, which could be solved by
<?php
$changeThese=array("<br>", "<br />", "<br/>");
$string = str_replace($changeThese," \n<br /> ",$string);
// so there is whitespace before running
$in=array('
((?:https?|ftp)://\S+[[:alnum:]]/?)si',
'((?<!//)(www\.\S+[[:alnum:]]/?))si');
$out=array('<a href="$1" >$1</a> ', '<a href="http://$1" >$1</a>');
return preg_replace($in,$out,$string);
?>
Known issue is that if there are existing HTML formatted hyperlinks, they will break.