How can regular expressions be utilized in PHP to search for and replace specific patterns within a string?

Regular expressions in PHP can be utilized with functions like preg_match() and preg_replace() to search for and replace specific patterns within a string. To search for a pattern, you can use preg_match() which returns true if the pattern is found, or false otherwise. To replace a pattern, you can use preg_replace() which replaces all occurrences of the pattern with a specified replacement.

$string = "Hello, World!";
$pattern = '/Hello/';
$replacement = 'Hi';

// Search for a pattern
if (preg_match($pattern, $string)) {
    echo "Pattern found in the string.";
} else {
    echo "Pattern not found in the string.";
}

// Replace a pattern
$newString = preg_replace($pattern, $replacement, $string);
echo $newString;