Skip to content

Commit

Permalink
Be more selective in what we make clickable.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.automattic.com/wordpress/branches/2.3@6450 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
ryan committed Dec 21, 2007
1 parent 2734f1c commit 5cd4fd2
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions wp-includes/formatting.php
Expand Up @@ -622,18 +622,36 @@ function antispambot($emailaddy, $mailto=0) {
return $emailNOSPAMaddy;
}

function _make_url_clickable_cb($matches) {
$url = $matches[2];
$url = clean_url($url);
if ( empty($url) )
return $matches[0];
error_log($matches[0], 0);
return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
}

function _make_web_ftp_clickable_cb($matches) {
$dest = $matches[2];
$dest = 'http://' . $dest;
$dest = clean_url($dest);
if ( empty($dest) )
return $matches[0];

return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>";
}

function _make_email_clickable_cb($matches) {
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}

function make_clickable($ret) {
$ret = ' ' . $ret;
// in testing, using arrays here was found to be faster
$ret = preg_replace(
array(
'#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
'#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
'#([\s>])([a-z0-9\-_.]+)@([^,< \n\r]+)#i'),
array(
'$1<a href="$2" rel="nofollow">$2</a>',
'$1<a href="http://$2" rel="nofollow">$2</a>',
'$1<a href="mailto:$2@$3">$2@$3</a>'),$ret);
$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
// this one is not in an array because we need it to run last, for cleanup of accidental links within links
$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
Expand Down

0 comments on commit 5cd4fd2

Please sign in to comment.