How can regular expressions be utilized in this scenario to improve the code?

The issue in the code is that it is using multiple string manipulation functions to extract specific parts of a string, which can be inefficient and error-prone. By using regular expressions, we can simplify the code and make it more robust.

// Original code
$string = "Hello, my name is John Doe and I am 30 years old.";
$words = explode(" ", $string);
$name = substr($words[4], 0, -1);
$age = substr($words[10], 0, -1);

// Improved code using regular expressions
$string = "Hello, my name is John Doe and I am 30 years old.";
preg_match('/name is (.+?) and/', $string, $matches_name);
preg_match('/am (\d+) years old/', $string, $matches_age);
$name = $matches_name[1];
$age = $matches_age[1];

echo "Name: $name\n";
echo "Age: $age\n";