Reputation: 173
I have this string:
An error page was displayed to the Web Services user.\nDetails: The Status you have chosen is invalid.\n\nStack Trace: Stack trace:
The string itself actually goes on and on for about another ~1000 characters. What I want to do is extract what's between Details:
and \n\Stack
. I'd end up with The Status you have chosen is invalid.
I think I have the preceding character removal using substr
:
<?php
$error = "An error page was displayed to the Web Services user.\nDetails: The Status you have chosen is invalid.\n\nStack Trace: Stack trace:.........";
$error2 = substr($error, 63);
echo $error2;
?>
But I always get an error syntax error, unexpected end of file
. I figured out that it's the \n
and \n\
that are throwing that error, but I have no control over those as they are returned to my script from an external API call. I've only defined the $error
here for illustrative purposes. This error is present even if I just echo $error
not just $error2
.
I've seen that I can do something like $short = substr($str, 0, strpos( $str, ' - Name:'));
for the trailing character removal, but I need to get the preceding working before I can do that.
Any advice appreciated!
Edit: I mentioned this in a comment, but the error string gets passed to my script from the API by $error = $e->getMessage();
.
Upvotes: 0
Views: 60
Reputation:
Use a regex with the m
and s
modifiers so that you can match newlines:
<?php
$error = "An error page was displayed to the Web Services user.\nDetails: The Status you have chosen is invalid.\n\nStack Trace: Stack trace:.........";
$result = preg_match("/^.*Details:\s(.*?)\n*Stack/ms", $error, $matches);
$errora = $matches[1];
echo $errora;
?>
Upvotes: 1