保存数据的时候,为了安全考虑,会用到这类功能,清除一串代码内所有的PHP代码。
应用案例
点击复制代码 PHP
$url="1asasdasdas<?php echo 'ssss'?>";
$name=RemovePHPCode($url);
print_r($name);
输出结果为:
1asasdasdas
函数
点击复制代码 PHP
function RemovePHPCode($code)
{
// PHP Start tags: <?php <? <?=
// PHP 5 supports: <% <script language="php">
// Depends on PHP
$continue = true;
while ($continue) {
$tokens = token_get_all($code);
$continue = false;
foreach ($tokens as $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;
}