How can PHP be used to extract and display specific data fields from a line of text?
To extract and display specific data fields from a line of text in PHP, you can use regular expressions to match and extract the desired information. By defining patterns that match the specific data fields you want to extract, you can then use functions like preg_match() to extract and display the data.
// Sample line of text containing data fields
$text = "Name: John Doe, Age: 30, Occupation: Developer";
// Define patterns to match specific data fields
$patterns = [
'/Name: (.*?),/',
'/Age: (.*?),/',
'/Occupation: (.*?)$/'
];
// Extract and display specific data fields
foreach ($patterns as $pattern) {
preg_match($pattern, $text, $matches);
echo $matches[1] . "\n";
}