note 76790 added to function.preg-match-all
| From: | aaron at parecki dot com | Date: | Tue, 31 Jul 2007 00:11:55 +0000 |
| Subject: | note 76790 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129061@lists.php.net to get a copy of this message | ||
<?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;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 67.168.215.179
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/76790
Del: integrated -- https://master.php.net/note/delete/76790/integrated
Del: useless -- https://master.php.net/note/delete/76790/useless
Del: bad code -- https://master.php.net/note/delete/76790/bad+code
Del: spam -- https://master.php.net/note/delete/76790/spam
Del: non-english -- https://master.php.net/note/delete/76790/non-english
Del: in docs -- https://master.php.net/note/delete/76790/in+docs
Del: other reasons-- https://master.php.net/note/delete/76790
Reject -- https://master.php.net/note/reject/76790
Search -- https://master.php.net/manage/user-notes.php