A very readable approach is to create an array with patterns and replacements, and then use array_keys and array_values in preg_replace
$replace = [ "1" => "one", "2" => "two", "3" => "three" ]; $content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
This even works with more complex patterns. The following code will replace 1, 2, and 3, and it will remove double spaces.
$replace = [ "1" => "one", "2" => "two", "3" => "three", "/ {2,}/" => " " ]; $content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
source share