Home  >  Article  >  Backend Development  >  How to remove backslashes in PHP?

How to remove backslashes in PHP?

藏色散人
藏色散人Original
2019-04-12 10:02:405101browse

本篇文章主要给大家介绍PHP去掉反斜杠的实现方法,那么在PHP中有一个内置函数stripslashes(),其作用就是删除字符串中的反斜杠。下面我们就结合具体的实例,给大家介绍PHP去掉反斜杠的方法,希望对需要的朋友有所帮助!

stripslashes()函数是PHP中的一个内置函数。此函数删除字符串中的反斜杠。

语法:

stripslashes(string)

参数:

该函数只接受一个参数,如上面的语法所示。

说明如下:

string:这是唯一需要的参数,指定函数将在其上操作的字符串。

返回值:

该函数返回一个去掉反斜杠的字符串。

代码示例1:

<?php 
   
    $str = "PHP and\ Python";
echo stripslashes($str);
?>

输出:

PHP and Python

代码示例2:

我们将看到stripslashes()函数的数组实现。stripslashes()不是递归的。为了将这个函数应用于数组,需要一个递归函数。

<?php 
function stripslashes_arr($value)
{
    $value = is_array($value) ?
        array_map(&#39;stripslashes_arr&#39;, $value) :
        stripslashes($value);

    return $value;
}


$array = array("PH\\P ", "an\\d", " \\Java");
$array = stripslashes_arr($array);


print_r($array);
?>

输出:

Array
(
    [0] => PHP
    [1] => and
    [2] =>  Java
)

相关推荐:《PHP教程

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