The PHP quotemeta is an inbuilt function in PHP. It accepts a string as an input and returns a string with a backslash in front of some characters. In this article, we will discuss the PHP quotemeta Function. Also, we will discuss a few examples of using it.
The pre-defined characters are:
- period (.)
- backslash (\)
- plus sign (+)
- asterisk (*)
- question mark (?)
- brackets ()
- caret (^)
- dollar sign ($)
- parenthesis (())
The function accepts only one parameter. The string in which we need to add backslash characters is input to the function.
The PHP quotemeta Function returns a string after quoting all metacharacters with a backslash (\).
Let’s discuss a few examples of using the function.
Example 1: When String Contains Dollar ($), Question Mark (?), and Period (.)
For instance, consider an example with a string containing Dollar ($), Question Mark (?), and Period (.).
<?php $string = 'The $ Value of INR is? Answer.'; echo quotemeta($string);; ?>
The function adds a backslash in front of all the pre-defined characters.
OUTPUT: The \$ Value of INR is\? Answer\.
Example 2: When String Contains Parenthesis (()), and Brackets ()
Similarly, the function adds backslashes in front of parenthesis and brackets.
<?php $string = 'Cat [Meow], Dog (Bark)'; echo quotemeta($string);; ?>
OUTPUT: Cat \[Meow\], Dog \(Bark\)
Example 3: When the String Contains Asterisk (*), and Plus Sign (+)
Similarly, we can pass a string containing Asterisk (*), and Plus Sign (+).
<?php $string = '1 + (2*2) = 5'; echo quotemeta($string);; ?>
OUTPUT: 1 \+ \(2\*2\) = 5
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.