PHP quotemeta() Function
Last Updated :
21 Jun, 2023
The quotemeta() function is an inbuilt function in PHP which accepts a string as an argument and returns a string which has backslashes added in front of some predefined characters in a string.
The predefined characters are:
- period (.)
- backslash (\)
- plus sign (+)
- asterisk (*)
- question mark (?)
- brackets ([])
- caret (^)
- dollar sign ($)
- parenthesis (())
Syntax:
quotemeta($string)
Parameter: This function accepts only one parameter $string which is mandatory. This parameter specifies the string in which we want to add backslashes in front of the above mentioned predefined characters.
Return Value: It returns a string by adding backslashes in front of the predefined characters in the $string argument.
Examples:
Input: $str = "geek$ for geeks?"
Output: geek\$ for geeks\?
Input: $str = "+geek* for geeks."
Output: \+geek\* for geeks\.
Below programs illustrate the quotemeta() function in PHP:
Program 1: When string has ‘?’ and ‘$’ predefined characters
<?php
$str = "geek$ for geeks?" ;
echo (quotemeta( $str ));
?>
|
Output:
geek\$ for geeks\?
Program 2: When string has ‘*’, ‘.’ and ‘+’ predefined characters
<?php
$str = "+geek* for geeks." ;
echo (quotemeta( $str ));
?>
|
Output:
\+geek\* for geeks\.
Program 3: When string has brackets and parenthesis as predefined characters.
<?php
$str = "[]geek for geeks()" ;
echo (quotemeta( $str ));
?>
|
Output:
\[\]geek for geeks\(\)
Program 4: When string has caret (^) as predefined character.
<?php
$str = "2 ^ 2 = 4" ;
echo (quotemeta( $str ));
?>
|
Output:
2 \^ 2 = 4
Reference:
http://php.net/manual/en/function.quotemeta.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...