Home  >  Article  >  Backend Development  >  How to use php quotemeta function?

How to use php quotemeta function?

藏色散人
藏色散人Original
2019-05-30 09:58:473052browse

php quotemeta函数用于在字符串中某些预定义的字符前添加反斜杠,其语法是quotemeta(string),参数string必需,指规定要检查的字符串。

How to use php quotemeta function?

php quotemeta函数怎么用?

定义和用法

quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠。

预定义的字符:

● 句号(.)

● 反斜杠(\)

● 加号(+)

● 星号(*)

● 问号(?)

● 方括号([])

● 脱字号(^)

● 美元符号($)

● 圆括号(())

提示:该函数可用于转义拥有特殊意义的字符,比如 SQL 中的 ( )、[ ] 以及 * 。

注释:该函数是二进制安全的。

语法

quotemeta(string)

参数 

string 必需。规定要检查的字符串。

返回值: 返回引用元字符的字符串。

PHP 版本: 4+

例子 1

在多个预定义字符前添加反斜杠:

<?php
$str1 = "1 + 1 = 2";
$str2 = "1 * 1 = 1";
$str3 = "Could you borrow me 5$?";
$str4 = "Are you not e&";
$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>";
?>

输出:

1 \+ 1 = 2
1 \* 1 = 1
Could you borrow me 5\$\?
Are you not e
The caret \[ \^ \] Looks like a hat!

例子 2

在预定义字符前添加反斜杠:

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

输出:

Hello world\. \(can you hear me\?\)

The above is the detailed content of How to use php quotemeta function?. 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
Previous article:How to enter phpMyAdminNext article:How to enter phpMyAdmin