note 41304 added to function.preg-replace
| From: | pull at hackthissite dot org | Date: | Tue, 06 Apr 2004 04:16:32 +0000 |
| Subject: | note 41304 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67689@lists.php.net to get a copy of this message | ||
<?php
//Code for the function
//Anti-XSS Script
// (Disallowing possibly harmful scripts)
function ptext($ar, $text){
if(!is_array($ar)){
//checks to see if input is array
$ar=str_replace("/","\/",$ar);
//allows / in user input
$num=strlen($ar);
//gets length of input
$i=0;
while($num!=$i){
$val=ord("$ar[$i]");
$art["$i"]='&#'.$val.';';
$arm .=$art[$i];
$i++;
}
/* The above breaks down the input
letter by letter
and converts it into an int
then changes it to &#NUM; format */
$arg=preg_replace("/$ar/i",$arm,$text);
//replaces the text
return $arg;
//returns formatted text
}
else{
$x=0;
$num2=count($ar);
//get number of words in input array
while($num2!=$x){
$arf="$ar[$x]";
//for breaking down into &#num; format
$nub=$ar[$x];
//for formatting with /$text/i
$nub=str_replace("/","\/",$nub);
//allowing / in input
$arre["$x"]="/$nub/i";
//formatting to /$text/i for preg_replace
$num=strlen($arf);
//below is the code for breaking
//down into &#num; format
$i=0;
while($num!=$i){
$val=ord("$arf[$i]");
$art["$i"]='&#'.$val.';';
$arm .=$art[$i];
$i++;
}
$rear["$x"]=$arm;
//replacement array
$arm=NULL;
//resets $arm
$x++;
}
$arg=preg_replace($arre, $rear, $text);
//replaces text
return $arg;
//returns formatted text
}
}
//## EXAMPLE USE ##//
$text='God money.....
ontext ON On oN
load eval java
load on eval ';
$pn=array("load","on","eval",'cookie',
'document','script','java');
$text=ptext($pn,$text);
//this would return God money.....
//ontext on on
//on
//
//load eval
//java
//load on
//eval
?>
Very usefull for guesbooks, user submitted pages, etc..
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41304
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41304&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41304&report=yes
Search -- http://master.php.net/manage/user-notes.php