How can the PHP code be optimized to efficiently extract the desired information from the string?

To efficiently extract the desired information from a string in PHP, you can use regular expressions to match the specific pattern you are looking for. By using preg_match() or preg_match_all() functions, you can easily extract the relevant data without unnecessary processing.

$string = "Product: Apple, Price: $1.99";
$pattern = '/Product: (\w+), Price: \$([\d\.]+)/';

if (preg_match($pattern, $string, $matches)) {
    $product = $matches[1];
    $price = $matches[2];
    
    echo "Product: $product, Price: $$price";
}