note 46866 added to function.preg-replace
| From: | richardathome at gmail dot com | Date: | Tue, 26 Oct 2004 09:23:03 +0000 |
| Subject: | note 46866 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79115@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:
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;
}
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46866
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46866&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46866&report=yes
Search -- http://master.php.net/manage/user-notes.php