How can preg_match_all be used to parse and extract data from a string in PHP?

To parse and extract data from a string in PHP, you can use the preg_match_all function along with regular expressions. This function allows you to search a string for multiple occurrences of a pattern and extract the matched data into an array for further processing.

<?php
$string = "Hello, my name is John and I am 25 years old. My friend's name is Sarah and she is 30 years old.";
$pattern = '/\b[A-Z][a-z]+\b/';
preg_match_all($pattern, $string, $matches);

print_r($matches[0]);
?>