note 46866 deleted from function.preg-replace by aidan

From: Date: Tue, 07 Dec 2004 03:10:22 +0000
Subject: note 46866 deleted from function.preg-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-81463@lists.php.net to get a copy of this message
Note Submitter: richardathome at gmail dot com ---- 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; } ?>

« previous php.notes (#81463) next »