The quotemeta Function

The quotemeta function adds a backslash before each special character in a string. The following characters are escaped: . \ + * ? [ ^ ] ( $ ). The function accepts one parameter - the string to process.

Syntax

phpquotemeta(string);

Example

Escaping special characters in a simple string:

php<?php
  echo quotemeta('Hello.world (test)');
?>

Code execution result:

php'Hello\.world \(test\)'

Example

Escaping a string containing multiple special characters:

php<?php
  echo quotemeta('1+1=2? [maybe]');
?>

Code execution result:

php'1\+1\=2\? \[maybe\]'

Example

Processing a string without special characters:

php<?php
  echo quotemeta('simple text');
?>

Code execution result:

php'simple text'

See Also

  • the addslashes function,
    which escapes a string with slashes
  • the preg_quote function,
    which escapes characters in regular expressions