Home  >  Article  >  Backend Development  >  PHP returns the function addcslashes() which refers to characters preceded by a backslash

PHP returns the function addcslashes() which refers to characters preceded by a backslash

黄舟
黄舟Original
2017-11-02 09:39:331936browse

Example

Add a backslash before the character "W":

<?php 
$str = addcslashes("Hello World!","W");
echo($str); 
?>

Definition and usage

addcslashes() function returns the specified string with a backslash added before the character.

Note: The addcslashes() function is case-sensitive.

Note: Apply addcslashes() to 0 (NULL), r (carriage return), n (line feed), t (form feed), f (tab) and v (vertical tab) Be careful when doing so. In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences.

Syntax

addcslashes(string,characters)
Parameters Description
string Required. Specifies the string to be escaped
characters Required. Specifies the characters or character range to be escaped.

Technical details

Return value: Returns the escaped string.
PHP version: 4+
##More examples

String Add a backslash to a specific character in the string:

<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,&#39;m&#39;)."<br>";
echo addcslashes($str,&#39;H&#39;)."<br>";
?>

Add a backslash to a range of characters in the string:

<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,&#39;A..Z&#39;)."<br>";
echo addcslashes($str,&#39;a..z&#39;)."<br>";
echo addcslashes($str,&#39;a..g&#39;);
?>

The following is a brief introduction to the usage of these two functions:

string addcslashes(string str,string charlist)

The first parameter str is the original string of the lost object

The second parameter charlist indicates which characters of the original string need to be preceded Add the character "\".

string

stripcslashes(string str)

Remove "\" in the string.

In addition, using the

addslashes function can also directly escape "'".

The example is as follows:

<?php
$sql = "update book set bookname=&#39;let&#39;s go&#39; where bookid=1";
 echo $sql."<br/>";
 $new_sql = addcslashes($sql,"&#39;");
 echo $new_sql."<br/>";
 $new_sql_01 = stripcslashes($new_sql);
 echo $new_sql_01."<br/>";
 echo addslashes($sql);
?>

The running result is as follows:

update book set bookname=&#39;let&#39;s go&#39; where bookid=1
update book set bookname=\&#39;let\&#39;s go\&#39; where bookid=1
update book set bookname=&#39;let&#39;s go&#39; where bookid=1
update book set bookname=\&#39;let\&#39;s go\&#39; where bookid=1


The above is the detailed content of PHP returns the function addcslashes() which refers to characters preceded by a backslash. 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