PHP returns the string function addslashes() that adds a backslash before the specified character.

PHP中文网
Release: 2023-03-16 21:16:01
Original
3417 people have browsed it

1. addslashes() function

1. The addslashes() function adds a backslash before the specified predetermined character. Syntax: addslashes(str);

2. The parameter is a string

3. There are four kinds of predefined characters: single quotation mark ('), double quotation mark ("), Backslash (\) and NULL

4. For example:

  <?php
    $str="Who&#39;s John Adams?";    echo $str."This is not safe in a database query.<br/>";//输出:Who&#39;s John Adams?This is not safe in a database query.
    echo addslashes($str)."This is safe in a database query.";//输出:Who\&#39;s John Adams?This is sage in a database query.
   ?>
Copy after login

12345678910

2. addcslashes() function

1.addcslashes () function adds a backslash before the specified character.

Syntax: addcslashes(str,chararcters);

2. The parameter str is required and specifies the character to be checked. String, and character is optional, specifying the characters or character range affected by addcslashes()

3. Example 1:

<?php
    $str="Hello,my name is John Adams.";
    echo $str;   //输出:Hello,my name is John Adams.echo addcslashes($str,&#39;m&#39;);  //输出:
    Hello,\my na\me is John Ada\ms.echo addcslashes($str,&#39;J&#39;);  //输出:Hello,my name is \John Adams
  ?>
Copy after login

123456789101112

Example. 2.

<?php$str="Hello,my name is John Adams.";
echo $str;  //输出:Hello,my name is John Adams.
echo addcslashes($str,&#39;A..Z&#39;);  //输出:\Hello,my name is \John \Adams.
echo addcslashes($str,&#39;a..z&#39;);  //输出:H\e\l\l\o,\m\y \n\a\m\e \i\s J\o\h\n A\d\a\m\s.
echo addcslashes($str,&#39;a..h&#39;);  //输出:H\ello,my n\am\e is Jo\hn A\d\ams.
?>
Copy after login

1234567891011121314

Note: The addcslashes() function is case-sensitive for the specified character or character range

in the character ". Add a backslash before W":

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

Definition and usage

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

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

Comments: 0 (NULL), r (carriage return), n (line feed), t (form feed), f (tab character). ) and v (vertical tab) be careful when applying addcslashes(). In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences

#. ##Syntax

addcslashes(string,characters)
Copy after login

Parameters

Description

string Required.

characters Required. Or a range of characters.

Technical Details

Return value:

Returns the escaped string

Adds inversion to specific characters in the string. Slash:

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

Example 2

Add backslash to a range of characters in a 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 above is the detailed content of PHP returns the string function addslashes() that adds a backslash before the specified character.. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!