How can I optimize my PHP code to efficiently handle and analyze strings with multiple attributes?

When handling strings with multiple attributes in PHP, it is essential to use regular expressions to efficiently extract and analyze the data. By using regular expressions, you can easily define patterns to match specific attributes within the string and extract them for further processing. Additionally, utilizing functions like preg_match_all() can help you efficiently extract all occurrences of a pattern from the string.

$string = "Name: John, Age: 30, Occupation: Developer";
$pattern = '/(\w+):\s*([^,]+)/';
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    $attribute = $match[1];
    $value = $match[2];
    
    echo "Attribute: $attribute, Value: $value\n";
}