What is "preg-replace"

preg_replace() is a PHP function that uses regular expressions (regex) to make string replacements.

Its general format is:

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject
               [, int $limit = -1 [, int &$count ]] ) : mixed

Its parameters are:

  • $pattern: a string containing the regular expression (or an array of strings, each of which is a regex), as described in documentation
  • $replacement: a string (or string array) with the value that will be used in the substitution
  • $subject: the string (or string array) in which the substitution will be made
  • $limit: the maximum number of substitutions that will be made in each string of the parameter $subject. The value default is -1 (indicating that there is no ceiling)
  • $count: if informed, this variable will be filled with the number of replacements made

If the $subject for an array, the function returns another array, containing the strings with the substitutions made. If $subject for a string, another string is returned, also with replacements made.

PHP has a regex PCRE (Perl Compatible Regular Expressions) engine and the syntax can be found on documentation. In the job documentation there is also several examples of use.


For more details about regular expressions, see wiki tag regex and the PHP documentation.