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 ww  w.  ja v a 2  s .  c  om
$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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions