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 => $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] => Database
[Error Message] => Error Executing Database Something is wrong.
[Error Detail] => [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?
",",".","|",":") \$\endgroup\$"-", PHP_EOLThe-to separate key from value, andPHP_EOLto create new item from string's new line \$\endgroup\$