PHP adds backslash function quotemeta() before certain predefined characters in the string

黄舟
Release: 2023-03-16 22:42:01
Original
1512 people have browsed it

Example

Add a backslash before predefined characters:

<?php
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
?>
Copy after login

Definition and usage

quotemeta() function adds some predefined characters in a string Add a backslash before the character.

Predefined characters:

  • Period (.)

  • Backslash (\)

  • Plus sign (+)

  • Asterisk (*)

  • Question mark (?)

  • Square brackets ([])

  • Caret (^)

  • Dollar sign ($)

  • Parentheses (())

Tip: This function can be used to escape characters with special meanings, such as ( ), [ ] in SQL as well as* .

Note: This function is binary safe.

Syntax

quotemeta(string)
Copy after login

Parameter Description

string Required. Specifies the string to check. ​

Technical details

Return value: ​ ​ Returns a string that quotes metacharacters.

PHP version: 4+

More examples

Example 1

Add backslashes before multiple predefined characters:

<?php
$str1 = "1 + 1 = 2";
$str2 = "1 * 1 = 1";
$str3 = "Could you borrow me 5$?";
$str4 = "Are you not entertained? (I am..)";
$str5 = "The caret [ ^ ] Looks like a hat!";

echo quotemeta($str1)."<br>";
echo quotemeta($str2)."<br>";
echo quotemeta($str3)."<br>";
echo quotemeta($str4)."<br>";
echo quotemeta($str5)."<br>";
?>
Copy after login


The above is the detailed content of PHP adds backslash function quotemeta() before certain predefined characters in the string. 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