note 76790 deleted from function.preg-match-all by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 01:56:08 +0000 |
| Subject: | note 76790 deleted from function.preg-match-all by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133081@lists.php.net to get a copy of this message | ||
Note Submitter: aaron at parecki dot com
----
<?php
/* Finds email addresses and urls in a body of text, and adds <a> tags
* around them. It will also obfuscate the email address with some
* javascript so that bots don't recognize the email address there.
*
* Include this Javascript code. This tells the browser how to decode
* the email address:
*
function swapPairs(s){
var res = "";
for (var i=0; i<s.length; i++){
var ch = s.charCodeAt(i) ;
res += String.fromCharCode(
( ch & 0xF0 ) +
((ch & 0x0C)>>2) +
((ch & 0x03)<<2)
);
}
return res;
}
*
*/
function ActivateLinks($text) {
$matches = array();
// Find all email addresses in the text
// regex based on http://www.regular-expressions.info/email.html
$regex = '/\b([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[a-zA-Z]{2,4})\b/';
preg_match_all($regex, $text, $matches);
if( count($matches[0]) > 0 ) {
foreach( $matches[0] as $email ) {
$obfuscate = '<a href="mailto:'.$email.'">'.$email.'</a>';
$encrypted = "";
for( $i=0; $i<strlen($obfuscate); $i++ ) {
$ch = ord(substr($obfuscate,$i,1));
$encrypted .= chr(
($ch & 0xF0) +
(($ch & 0x0C) >> 2) +
(($ch & 0x03) << 2)
);
}
$replace =
'<script>document.write(swapPairs("'.$encrypted.'"))</script>';
$text = str_replace($email, $replace, $text);
}
}
// Find all http or ftp links in the text
// regex from http://fundisom.com/phparadise/php/string_handling/autolink
$text = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i',
'<a href="\0">\4</a>', $text );
return $text;
}
?>