Saturday, November 22, 2025
HomeLanguagesPHP quotemeta() Function

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 neveropen?"
Output: geek\$ for neveropen\?

Input: $str = "+geek* for neveropen."
Output: \+geek\* for neveropen\.

Below programs illustrate the quotemeta() function in PHP:

Program 1: When string has ‘?’ and ‘$’ predefined characters




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


Output:

geek\$ for neveropen\?

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




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


Output:

\+geek\* for neveropen\.

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




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


Output:

\[\]geek for neveropen\(\)

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




<?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));
?>


Output:

2 \^ 2 = 4

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

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS