php quotemeta函数用于在字符串中某些预定义的字符前添加反斜杠,其语法是quotemeta(string),参数string必需,指规定要检查的字符串。
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)."
";
echo quotemeta($str2)."
";
echo quotemeta($str3)."
";
echo quotemeta($str4)."
";
echo quotemeta($str5)."
";
?>
输出: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\?\)