php清除一串代码内所有的PHP代码
保存数据的时候,为了安全考虑,会用到这类功能,清除一串代码内所有的PHP代码。
应用案例
$url="1asasdasdas<?phpecho'ssss'?>"; $name=RemovePHPCode($url); print_r($name);
输出结果为:
1asasdasdas
函数
functionRemovePHPCode($code)
{
//PHPStarttags:<?php<?<?=
//PHP5supports:<%<scriptlanguage="php">
//DependsonPHP
$continue=true;
while($continue){
$tokens=token_get_all($code);
$continue=false;
foreach($tokensas$tt){
$name=is_numeric($tt[0])?token_name($tt[0]):'';
if($name==='T_OPEN_TAG'||$name==='T_OPEN_TAG_WITH_ECHO'||$name==='T_CLOSE_TAG'){
$code=str_replace($tt[1],"",$code);
$continue=true;
}
}
}
return$code;
} 

