preg_match在PHP中不匹配,尽管正则表达式似乎是正确的

时间:2012-08-15 11:20:14

标签: php preg-match

我在做

preg_match("/\<\/p\> (\d*?) /", $error, $matches)

其中$error="bla-bla-bla </p> 12345 bla-bla-bla"

奇怪的是,即使我仔细检查正则表达式,它也找不到匹配。

为什么不起作用?

以下是完整的代码:

            $values=$form->getValue();
            $url = "http://something.freshdesk.com/helpdesk/tickets.xml";
            $request = "<helpdesk_ticket><description>".$values['subject']."</description><email>".$username."</email></helpdesk_ticket>"; 
            echo $request;
            $headers = array('Content-Type: application/xml','Content-Length: ' . strlen($request));
            var_dump($headers);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_USERPWD, 'email@email.com:password');
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
//          curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 

            $http_result = curl_exec($ch);
            $error       = curl_error($ch);
            $http_code   = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

            curl_close($ch);

            if ($error) {
              print "<br /><br />$error";
            } else {
              if (preg_match("/\<\/p\> (\d*?) /", $error, $matches)) {
                $ticket_id = $matches[1];
                print "Ticket submitted: $ticket_id\n";
              }
              print "<br /><br />Location header not found";
              var_dump($matches);
              var_dump($http_result);
              echo $error;
              echo $http_result;

2 个答案:

答案 0 :(得分:1)

你的正则表达式是正确的,它肯定是匹配的。您的问题是您没有在http响应时执行正则表达式。使用$http_result代替$error

preg_match("/\<\/p\> (\d*)/", $http_result, $matches);

答案 1 :(得分:0)

找到解决方案:

我应该在htmlspecialchars()

上使用$error
相关问题