What is the best way to extract information from multiple strings in PHP?

When extracting information from multiple strings in PHP, one effective way is to use regular expressions. Regular expressions allow you to define patterns to match specific parts of the strings you are working with. By using functions like preg_match or preg_match_all, you can extract the desired information from each string efficiently.

<?php
// Sample strings
$string1 = "Name: John, Age: 25";
$string2 = "Name: Jane, Age: 30";

// Define the pattern to match name and age
$pattern = '/Name: (\w+), Age: (\d+)/';

// Extract information from string 1
preg_match($pattern, $string1, $matches1);
$name1 = $matches1[1];
$age1 = $matches1[2];

// Extract information from string 2
preg_match($pattern, $string2, $matches2);
$name2 = $matches2[1];
$age2 = $matches2[2];

// Output the extracted information
echo "Name: $name1, Age: $age1\n";
echo "Name: $name2, Age: $age2\n";
?>