首页 > 开发 > PHP > 正文

PHP如何去掉反斜杠?

2024-05-04 22:02:02
字体:
来源:转载
供稿:网友

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

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

语法:

stripslashes(string)

参数:

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

说明如下:

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

返回值:

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

代码示例1:

  1. <?php  
  2.  
  3. $str = "PHP and/ Python"
  4.  
  5. echo stripslashes($str); 
  6.  
  7. ?> 

输出:

PHP and Python

代码示例2:

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

  1. <?php  
  2.  
  3. function stripslashes_arr($value
  4.  
  5.  
  6.     $value = is_array($value) ? 
  7.  
  8.         array_map('stripslashes_arr'$value) : 
  9.  
  10.         stripslashes($value); 
  11.  
  12.     return $value
  13.  
  14.  
  15.  
  16. $array = array("PH//P ""an//d"" //Java"); 
  17.  
  18. $array = stripslashes_arr($array); 
  19.  
  20.  
  21. print_r($array); 
  22.  
  23. ?> 

输出:

  1. Array 
  2.  
  3.  
  4.     [0] => PHP 
  5.  
  6.     [1] => and 
  7.  
  8.     [2] =>  Java 
  9.  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表