PHP quotemeta Function | Quote Meta Characters

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 (())

Syntax

quotemeta($string)

Parameters

The function accepts only one parameter. The string in which we need to add backslash characters is input to the function.

Return Value

The PHP quotemeta Function returns a string after quoting all metacharacters with a backslash (\).

PHP quotemeta Function

Examples

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

Conclusion

In conclusion, we discussed the PHP quotemeta Function. You can read more about it on the Official PHP Documentation. Additionally, you can read about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *