Home > Backend Development > PHP Tutorial > 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

黄舟
Release: 2023-03-16 21:38:01
Original
2096 people have browsed it

Example

Add a backslash before the character "W":

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

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)
Copy after login
ParametersDescription
string Required. Specifies the string to be escaped
charactersRequired. 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>";
?>
Copy after login

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;);
?>
Copy after login

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);
?>
Copy after login

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
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template