PHP | quotemeta() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the 
// working of quotemeta() function 
  
$str = "geek$ for geeks?";
  
// prints the string by adding backslashes 
// in front of the predefined characters
// '$' and '?'
echo(quotemeta($str));
?>

chevron_right


Output:

geek\$ for geeks\?

Program 2: When string has ‘*’, ‘.’ and ‘+’ predefined characters

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the 
// working of quotemeta() function 
  
$str = "+geek* for geeks.";
  
// prints the string by adding backslashes 
// in front of the predefined characters
echo(quotemeta($str));
?>

chevron_right


Output:

\+geek\* for geeks\.

Program 3: When string has brackets and paranthesis as predefined characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the 
// working of quotemeta() function 
  
$str = "[]geek for geeks()";
  
// prints the string by adding backslashes 
// in front of the predefined characters
// brackets and paranthesis
echo(quotemeta($str));
?>

chevron_right


Output:

\[\]geek for geeks\(\)

Program 4: When string has caret (^) as predefined character.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the 
// working of quotemeta() function 
  
$str = "2 ^ 2 = 4";
  
// prints the string by adding backslashes 
// in front of the predefined characters
// caret (^)
echo(quotemeta($str));
?>

chevron_right


Output:

2 \^ 2 = 4

Reference:
http://php.net/manual/en/function.quotemeta.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.