How can regular expressions be used effectively in PHP to manipulate strings like in the provided code example?

Regular expressions can be used effectively in PHP to manipulate strings by defining patterns that match specific parts of the string. This allows for searching, replacing, and extracting substrings based on the defined pattern. In the provided code example, regular expressions are used to extract all numbers from a string and store them in an array.

$string = "I have 3 apples, 2 oranges, and 5 bananas.";
preg_match_all('/\d+/', $string, $matches);
$numbers = $matches[0];

print_r($numbers);