1

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?

9
  • I can only imagine that the content of $ready from adjusting $string actually 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? Commented Nov 7, 2017 at 21:09
  • 1
    @Randall actually I can: Error Type - Database Error Message - Error Executing Database Query. Error Detail - [Macromedia][SQLServer JDBC Driver][SQLServer]String or binary data would be truncated. Commented Nov 7, 2017 at 21:11
  • @Randall that would be the same error as the one at the top Commented Nov 7, 2017 at 21:11
  • 2
    I'm thinking what PHP_EOL thinks is the return, is not what is in the $string, thus its just not str_replacing them with dashes? Commented Nov 7, 2017 at 21:12
  • 2
    The API you're using is presumably not written in PHP, so it doesn't use the same newline convention as PHP does. Commented Nov 7, 2017 at 21:16

1 Answer 1

2

The system you're dealing with apparently uses a different newline character than the system running the PHP script. I suggest you handle all types of newlines:

$delimiters = array('-', "\r\n", "\n", "\r");
Sign up to request clarification or add additional context in comments.

1 Comment

Add "chr(13)" to the end of that array. And also for precaution run this after str_replace: $string = preg_replace("/(-){2,}/","-",$string); to truncate multiple dashes into 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.