note 95976 deleted from function.preg-replace by thiago
| From: | thiago@php.net | Date: | Mon, 01 Feb 2010 12:35:25 +0000 |
| Subject: | note 95976 deleted from function.preg-replace by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165553@lists.php.net to get a copy of this message | ||
Note Submitter: Aleksandr Tsertkov s6urik at gmail dot com
----
Here is an example of configurable regular expression for converting urls to html links from http://www.devhands.com/2010/02/php-preg_replace-url/
<?php
// configurable delimiters and schemes
$delimiters = '\\s"\\.\',';
$schemes = 'https?|ftps?';
// build pattern
$pattern = sprintf('#(^|[%s])((?:%s)://\S+[^%1$s])([%1$s]?)#i', $delimiters, $schemes);
$replacement = '$1<a href="$2">$2</a>$3';
$subjects = array(
'http://localhost',
'prefix "http://example.com/path&var=val"
suffix',
// ...
);
foreach ($subjects as $subject) {
var_dump(preg_replace($pattern, $replacement, $subject));
}
?>