9951 explained code solutions for 126 technologies


php-regexHow to replace strings using PHP regex?


PHP provides a powerful set of regular expression functions that can be used to replace strings.

$string = 'This is a string';
$pattern = '/string/';
$replacement = 'sentence';

echo preg_replace($pattern, $replacement, $string);

Output example

This is a sentence

The code above uses the preg_replace() function to replace the string string with sentence in the variable $string. The $pattern variable contains the regular expression pattern to match the string to be replaced, and the $replacement variable contains the string to replace it with.

The preg_replace() function takes three parameters: the regular expression pattern, the replacement string, and the string to be searched. It returns the modified string.

Helpful links

Edit this code on GitHub