Home >Backend Development >PHP Problem >How to remove backslashes in escaped strings in php

How to remove backslashes in escaped strings in php

藏色散人
藏色散人Original
2020-08-11 09:57:473226browse

php去掉斜杠的实现方法:首先创建一个PHP示例文件;然后定义一个“delete_fxg”方法;接着通过“$array[$k] = stripslashes($v);”方法去掉反斜杠字符即可。

How to remove backslashes in escaped strings in php

推荐:《PHP视频教程

PHP去掉转义后字符串中的反斜杠\函数stripslashes 

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);
?>

The above is the detailed content of How to remove backslashes in escaped strings 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