I am dealing with a system which sends any unexpected errors back to me in a string with the error type, message, and details, for example:
Error Type - Database
Error Message - Error Executing Database Query.
Error Detail - [Macromedia][SQLServer JDBC Driver][SQLServer]String or binary data would be truncated.
Now to create a better UI I am expected to deal with these errors, so I thought about 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:
private function string_to_array( $delimiters, $string )
{
// start by replacing the $delimiters
$ready = str_replace( $delimiters, $delimiters[0], $string );
// create new list() item:
// $key => $value
list( $err_typ, $err_typ_cont,
$err_mess, $err_mess_cont,
$err_det, $err_det_cont ) = explode( $delimiters[0], $ready ); # line 238
$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;
}
To call the functions I am using array('-', PHP_EOL) as $delimiters and the $string is the error message as a string
It worked really well when I inserted a random error into a string and called the function. However, when I trigger a random error from the server, I start to run into issues, specifically, Undefined offset for my list() function
PHP error
<br />
<b>Notice</b>: Undefined offset: 5 in <b>C:\xampp\htdocs\application\_backend\class\class.program.php</b> on line <b>238</b><br />
<br />
<b>Notice</b>: Undefined offset: 4 in <b>C:\xampp\htdocs\application\_backend\class\class.program.php</b> on line <b>238</b><br />
Also, note that the array comes all weird and wrong:
Response in an array format
Array
(
[Error Type] => Database
Error Message
[Error Executing Database Query.
Error Detail] => [Macromedia][SQLServer JDBC Driver][SQLServer]String or binary data would be truncated.
[] =>
)
This is the same error as I can see above.
How can I fix that? What am I missing here?
$readyfrom adjusting$stringactually cannot explode into 6 items... like the delimiters are not matching up. Can you print_r() both of those before the explode to debug what they think they have?Error Type - Database Error Message - Error Executing Database Query. Error Detail - [Macromedia][SQLServer JDBC Driver][SQLServer]String or binary data would be truncated.PHP_EOLthinks is the return, is not what is in the$string, thus its just not str_replacing them with dashes?