How can one handle errors or unexpected data when parsing strings in PHP, such as missing delimiters like '='?

When parsing strings in PHP, such as key-value pairs separated by '=', it is important to handle errors or unexpected data, like missing delimiters. One way to handle this is by checking if the delimiter exists in the string before attempting to parse it. If the delimiter is missing, you can either skip that particular data or handle it in a way that suits your application.

$data = "key1=value1&key2value2&key3=value3";

$pairs = explode('&', $data);

foreach ($pairs as $pair) {
    if (strpos($pair, '=') !== false) {
        list($key, $value) = explode('=', $pair);
        echo "Key: $key, Value: $value" . PHP_EOL;
    } else {
        echo "Missing delimiter in pair: $pair" . PHP_EOL;
    }
}