Having some trouble with what seems a simple match operation. I'm pretty sure some knowledgeable person knows what i'm missing here...
$errors = array(
"Error: Unrecognized command found at '^' position",
);
if (in_array($result, $errors)) {
//do something
}
When I echo out $result, it returns
"Error: Unrecognized command found at '^' position"
But it is not found in the array. When i echo out as json:
"^\r\nError: Unrecognized command found at '^' position"
I tried using preg_replace to remove ^\\r\\n, but still no match. Any ideas?
I believe the issue is because you were looking for the \\r
and \\n
special characters instead of litterals. You can use the \\
escape character to fix this:
^\\\\r\\\\n
You can use ltrim
function and explicitly specify the characters you want to remove, including ^
:
$str = ltrim("^\r\nError: Unrecognized command found at '^' position", "^\r\n");
var_dump($str);
// string(49) "Error: Unrecognized command found at '^' position"
Try using str_replace
Please find the below snippet which works absolutely fine:
$errors = array(
"Error: Unrecognized command found at '^' position",
);
$result = "\r\nError: Unrecognized command found at '^' position";
$result = str_replace("\r\n", '', $result);
if (in_array($result, $errors)) {
echo 'Hello World!';
}
Output:
Hello World!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.