← All built-ins / preg_replace()

preg_replace()

preg_replace(...$args): string

Parameters

NameTypeDescription
$patternstring/…/ pattern
$replacementstringreplacement; $1..$n reference capture groups
$subjectstringstring to modify

Return value

The string with every match replaced.

Example

echo preg_replace("/\s+/", "_", "holy php lang"), "
";
echo preg_replace("/(\w+)@(\w+)/", "$2/$1", "ada@holy"), "
";
holy_php_lang
holy/ada

See also

str_replace()preg_match()