使用 preg_match 测试的 cURL Return 失败
Return from cURL tested with preg_match fails
我从 cURL 返回一个响应数组,我想在其中根据正则表达式模式测试正文。这是一个示例数组:
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] => )
这是我的 php if 语句的样子:
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", $result['body'])) {
die ("preg_match failed");
}
问题:为什么 Die() 会被触发?
此处的测试模式按预期工作。
http://www.phpliveregex.com/p/b2W
奇怪,因为这在我的本地主机上有效,但在生产服务器上无效。
Php 版本是:PHP 5.3.10-1ubuntu3.18 with Suhosin-Patch
您的值中可能有一些空格,这就是它与模式不匹配的原因。
要解决此问题,只需在 preg_match()
中使用 trim()
,例如
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", trim($result['body']))) {
//^^^^^
die ("preg_match failed");
}
我从 cURL 返回一个响应数组,我想在其中根据正则表达式模式测试正文。这是一个示例数组:
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] => )
这是我的 php if 语句的样子:
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", $result['body'])) {
die ("preg_match failed");
}
问题:为什么 Die() 会被触发?
此处的测试模式按预期工作。 http://www.phpliveregex.com/p/b2W
奇怪,因为这在我的本地主机上有效,但在生产服务器上无效。
Php 版本是:PHP 5.3.10-1ubuntu3.18 with Suhosin-Patch
您的值中可能有一些空格,这就是它与模式不匹配的原因。
要解决此问题,只需在 preg_match()
中使用 trim()
,例如
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", trim($result['body']))) {
//^^^^^
die ("preg_match failed");
}