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


preg_match does not match in PHP, although the regular expression seems to be correct

我正在做

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;

您的正则表达式是正确的,并且肯定匹配。 您的问题是您没有在 http 的响应时执行正则表达式。使用$http_result而不是$error

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

找到了解决方案:

我应该在$error上使用htmlspecialchars()