PHP Tutorial - PHP quotemeta() Function






Definition

The quotemeta() function adds backslashes 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 (())

This function can be used to escape characters in SQL.





Syntax

PHP quotemeta() Function has the following syntax

quotemeta(string)

Parameter

ParameterIs Required Description
stringRequired. String to check

Return

PHP quotemeta() Function return the string with meta characters quoted.

Example

Add backslashes in front of many predefined characters:


<?php//from w ww .j av  a  2s  .c  o m
$str = "Hello world. (from java2s.com?)";
echo quotemeta($str);

$str1 = "1 + 1 = 2";
$str2 = "1 * 1 = 1";
$str3 = "5$?";
$str4 = "java2s.com$?(...)";
$str5 = "^";

echo quotemeta($str1)."\n";
echo quotemeta($str2)."\n";
echo quotemeta($str3)."\n";
echo quotemeta($str4)."\n";
echo quotemeta($str5)."\n";
?>

The code above generates the following result.