Home >Backend Development >PHP Problem >How to remove backslash from escaped characters in php

How to remove backslash from escaped characters in php

coldplay.xixi
coldplay.xixiOriginal
2021-03-02 18:06:312647browse

php去除转义字符中的反斜杠的方法:使用函数stripslashes去掉转义后字符串中的反斜杠,代码为【if (is_string($v)) {$array[$k] = stripslashes($v);}】。

How to remove backslash from escaped characters in php

本教程操作环境:windows7系统、PHP5.6版,DELL G3电脑。

php去除转义字符中的反斜杠的方法:

addslashes函数主要是在字符串中添加反斜杠对特殊字符进行转义,stripslashes则是去掉转义后字符串中的反斜杠\,比如当你提交一段json数据到PHP端的时候可能会遇到json字符串中有\导致json_decode函数无法将json数据转换成数组的情况,这时你就需要stripslashes函数。

该函数用于清理从数据库或 HTML 表单中取回的数据。

例子

输出:

Who's John Adams?
<?php
function delete_fxg(&$array) {
         while(list($k,$v) = each($array)) {
               if (is_string($v)) {
                   $array[$k] = stripslashes($v);//去掉反斜杠字符
               }
               if (is_array($v))  {
                   $array[$k] = delete_fxg($v);//调用本身,递归作用
              }
        }
        return $array;
}
$str[0][1]="123123\\\\";
$str[0][2]="456456\\\\";
delete_fxg($str);
print_r($str);
?>

相关视频推荐:PHP视频教程

The above is the detailed content of How to remove backslash from escaped characters in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn