How can PHP be used to extract specific information, such as dates, from text strings efficiently?

To extract specific information, such as dates, from text strings efficiently in PHP, you can use regular expressions. Regular expressions allow you to define patterns to match specific text formats, making it easier to extract the desired information. By using functions like preg_match() or preg_match_all(), you can search for and extract dates from text strings based on a specified pattern.

$text = "Today is 2022-01-15 and tomorrow is 2022-01-16.";
$pattern = "/\d{4}-\d{2}-\d{2}/";
preg_match_all($pattern, $text, $matches);

foreach($matches[0] as $date){
    echo $date . "\n";
}