当前位置:首页 > PHP教程 > PHP高级教程

php addslashes 利用递归实现使用反斜线引用字符串

实现代码:

复制代码 代码如下:

<?php
function addslashes_deep($value)
{
//史上最经典的递归,一行搞定
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}

//测试数据
$_post['str'] = "'fanglor ' is a  boy  >'";
$_get['str1'] = 'fanglor " is a  boy  >';

echo '当前get_magic_quotes_gpc为  '.get_magic_quotes_gpc();
echo "<br/>";

//判断当前是否开启get_magic_quotes_gpc
if (!get_magic_quotes_gpc()){
$_post = addslashes_deep($_post);
$_get = addslashes_deep($_get);
$_cookie = addslashes_deep($_cookie);
}

//打印结果
var_dump ($_post);
echo "<br/>";
var_dump ($_get);

?>


打印结果:
当前get_magic_quotes_gpc为 0
array(1) { ["str"]=> string(30) "'fanglor ' is \ a boy >'" }
array(1) { ["str1"]=> string(26) "fanglor " is \ a boy >" }


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!