Home >Backend Development >PHP Tutorial >PHP returns the string function addslashes() that adds a backslash before the specified character.

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

PHP中文网
PHP中文网Original
2017-11-01 10:24:593486browse

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

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

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

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

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)

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

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

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!

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