PHP quotemeta() 函数
实例
预定义字符前添加反斜杠:
<?php str = "Hello world. (can you hear me?)"; echo quotemeta($str); ?>
Definition and usage
quotemeta() function adds a backslash before some predefined characters in a string.
Predefined characters:
- Period (.)
- Backslash (\)
- Plus (+)
- Asterisk (*)
- Question mark (?)
- Square brackets ([ ])
- Tilde (^)
- Dollar sign ($)
- Parentheses ()
Tip:This function can be used to escape special characters, such as ( ) in SQL, [ ] and * .
Note:This function is binary safe.
Syntax
quotemeta(string)
Parameter | Description |
---|---|
string | Required. Specify the string to be checked. |
Technical details
Return value: | Return the string of the referenced meta character. |
PHP version: | 4+ |
More examples
Example 1
Multiple predefined characters before adding backslash:
<?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>"; ?>