Reputation: 2043
From cURL I´m getting back an response array where I´d like to test the body against a regex pattern. Here is a sample array:
Array ( [body] => 9068205463|admin [headers] => Array ( [0] => HTTP/1.1 200 OK [1] => Date: Mon, 04 May 2015 16:45:56 GMT [2] => Server: Apache [3] => Vary: Accept-Encoding [4] => Content-Encoding: gzip [5] => Content-Length: 38 [6] => Connection: close [7] => Content-Type: text/html ) [engine] => cURL [cached] => )
Here is what my php if statement looks like:
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", $result['body'])) {
die ("preg_match failed");
}
Question: Why is Die() fired?
Testing pattern here works like expected. http://www.phpliveregex.com/p/b2W
Strange as this is working on my localhost but not on a production server.
Php Version is: PHP 5.3.10-1ubuntu3.18 with Suhosin-Patch
Upvotes: 1
Views: 147
Reputation: 59701
You probably have some spaces in your value and that's the reason why it doesn't matches the pattern.
To fix this simply use trim()
in preg_match()
, e.g.
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", trim($result['body']))) {
//^^^^^
die ("preg_match failed");
}
Upvotes: 1