note 46866 modified in function.preg-replace by aidan
| From: | aidan@php.net | Date: | Sun, 05 Dec 2004 15:16:52 +0000 |
| Subject: | note 46866 modified in function.preg-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-81402@lists.php.net to get a copy of this message | ||
There is a minor bug in cgamedude's otherwise excellent url to hyperlink routine which fails on
XHTML encoded url's that contain parameters.
A semi-colon is missing from the $subdir regex. Here is the updated function:
<?php
function convert_links ($text)
{
// First match things beginning with http:// (or other protocols)
$not_anchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$protocol = '(http|ftp|https):\/\/';
$domain = '[\w]+(.[\w]+)';
$subdir = '([\w\-\.;,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$expr = '/' . $not_anchor . $protocol . $domain . $subdir . '/i';
$result = preg_replace( $expr, "<a href='$0'
title='$0'>[link]</a>", $text );
// Now match things beginning with www.
$not_anchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$not_http = '(?<!:\/\/)';
$domain = 'www(.[\w]+)';
$subdir = '([\w\-\.;,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$expr = '/' . $not_anchor . $not_http . $domain . $subdir . '/i';
$result=preg_replace( $expr, "<a href='http://$0'
title='http://$0'>[link]</a>", $result );
return $result;
}
?>
--was--
There is a minor bug in cgamedude's otherwise excellent url to hyperlink routine which fails on
XHTML encoded url's that contain parameters.
A semi-colon is missing from the $subdir regex. Here is the updated function:
function convert_links ($text) {
// First match things beginning with http:// (or other protocols)
$not_anchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$protocol = '(http|ftp|https):\/\/';
$domain = '[\w]+(.[\w]+)';
$subdir = '([\w\-\.;,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$expr = '/' . $not_anchor . $protocol . $domain . $subdir . '/i';
$result = preg_replace( $expr, "<a href='$0'
title='$0'>[link]</a>", $text );
// Now match things beginning with www.
$not_anchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$not_http = '(?<!:\/\/)';
$domain = 'www(.[\w]+)';
$subdir = '([\w\-\.;,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$expr = '/' . $not_anchor . $not_http . $domain . $subdir . '/i';
$result=preg_replace( $expr, "<a href='http://$0'
title='http://$0'>[link]</a>", $result );
return $result;
}
http://php.net/manual/en/function.preg-replace.php