How can regular expressions be used in PHP to manipulate strings?

Regular expressions can be used in PHP to manipulate strings by searching for specific patterns within a string and replacing or extracting certain parts of the string based on those patterns. This can be useful for tasks such as validating input, formatting text, or extracting data from a string.

// Example of using regular expressions in PHP to manipulate strings
$string = "Hello, my email is example@email.com";
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
$replacement = "REDACTED_EMAIL";

$newString = preg_replace($pattern, $replacement, $string);

echo $newString; // Output: Hello, my email is REDACTED_EMAIL