How can preg_replace be used to replace specific patterns in a string in PHP?
To replace specific patterns in a string in PHP, you can use the preg_replace function. This function allows you to search for a specific pattern using a regular expression and replace it with a specified value. By using preg_replace, you can easily manipulate strings by replacing certain patterns with desired values.
$string = "Hello, my name is [NAME] and I am [AGE] years old.";
$pattern = "/\[NAME\]/";
$replacement = "John";
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string;