PHP string quotemeta() function
The quotemeta() is an in-built function of PHP, which is used to add a backslash before some meta characters in the string. It returns the string by adding the backslash before every meta characters. It avoids SQL injections attacks of our database. The quotemeta() is a binary-safe function, means that type of function which can be executed on a binary file without modifying its content.
The predefined meta character are:
- (.) – Period Plus
- (+) – Plus
- (*) – Asterisk
- (?) – Question mark
- ([ ]) – Square Brackets Caret
- (^) – Caret
- ($) – Dollar
- (( )) – Parenthesis
Note: This function can be used to escape the character with special meaning.
Syntax
The syntax for the quotemeta() function is given below, which has only one string parameter:
Parameter
String (required) – This function has a single parameter, which is string. We provide string parameter in this function to quote backslash before every meta character which is listed above in the tutorial.
Return Values
The quotemeta() function returns the string by adding the backslash () before every meta character which is listed above or return FALSE if the given string is empty.
Example
To understand the practical implementation of the quotemeta() function, see the examples which are given below:
Example 1
Output:
Let’s check the below output to see that backslash is added in front of every meta character.
Hello user! Welcome to TutorAspire . Would you like to learn PHP?
Example 2
Output:
Let’s check the below example that backslash () is added before every meta character.
Dollar symbol $ is used to declare variable in PHP. Matrix is represented by square [ ] brackets.
Example 3
Output:
equation1 =/(m - n/) /^o /* p