Using PHP explode() to parse multi-part error messages I am dealing with a system which sends any unexpected errors back to me in a string, for example:

Error Type - Database
Error Message - Error Executing Database Something is wrong.
Error Detail - [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near '1'.


Now to create a better UI I am expected to deal with these errors, so I thought about using explode(), and found this user's response in the PHP Manual which helped me delimit the results with two delimiters instead of just one:

Then I needed to get a little deeper into the code because I wanted $key =&gt; $val pairs, so I came across this answer on StackOverflow.

Then I combined both and came across this solution:

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);

    list($err_typ, $err_typ_cont,
         $err_mess, $err_mess_cont,
         $err_det, $err_det_cont) = explode($delimiters[0], $ready);

    $result[ trim($err_typ) ] = trim($err_typ_cont);
    $result[ trim($err_mess) ] = trim($err_mess_cont);
    $result[ trim($err_det) ] = trim($err_det_cont);
    return  $result;
}


Then I call the function like this:

 multiexplode(array("-", PHP_EOL), $errorMessage)


The response seems to be exactly what I needed:

Array
(
    [Error Type] =&gt; Database
    [Error Message] =&gt; Error Executing Database Something is wrong.
    [Error Detail] =&gt; [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near '1'.
)




My question comes with PHP's best practices, and if there would be a more reliable or a more efficient approach I could take into solving this problem?
A couple of thoughts:


This function is designed to only deal with a very sepcific use case - parsing a formatted error string.  For that reason I would recommend changing the function name to be more representative of what is actually happening.  Perhaps something like parseErrorString() or similar.
Since you are dealing with a very specific use case here, why would you require the caller to pass in the delimiters?  This function should fully encapsulate all the parsing logic.
Using an array for return, seems odd, particularly since you have spaces in your key names.  I would consider returning a stdClass or similar object with type, message, detail properties.
Though not strictly required, I would consider using regular expressions here, as I think that can clean things up as well as allow the solution to be less fragile to changes in the string format.
Using PHP_EOL as line break may be fragile. PHP_EOL is good for working with strings within same environment, but may fail if your application is running in a different environment than the environment your string is produced in.
Your function assumes happy path execution.  What if you get a poorly/unexpectedly formatted string?  Perhaps you are validating this string somewhere else, but if not, you should do it here in your function.


A refactoring, with these thoughts in mind, might yield something like the following:

function parseErrorString($string) {
    if(empty($string) || !is_string($string)) {
        throw new InvalidArgumentException('Non-zero length string expected');
    }
    $lines = preg_split('/\R/', $string);
    $error = array_reduce(
        $lines,
        function (&amp;$err, $line) {
            $regex = '/^Error (Type|Message|Detail) - (.*)$/';
            $matches = [];
            $result = preg_match($regex, $line, $matches);
            if ($result !== 1) {
                // no match.  Is this an error condition?
                // at the very least, do nothing
                return $err;
            }
            $err-&gt;{strtolower($matches[1])} = $matches[2];
            return $err;
        },
        new stdClass();
    );
    return $error;
}