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

preg_match在PHP中不匹配,尽管正则表达式似乎是正确的,php,preg-match,Php,Preg Match,我在做什么 preg_match("/\<\/p\> (\d*?) /", $error, $matches) preg\u match(“/\(\d*?)/”,$error,$matches) 其中$error=“bla bla bla12345 bla bla bla bla” 奇怪的是它没有找到匹配项,即使我仔细检查了正则表达式 为什么不起作用 以下是完整的代码: $values=$form->getValue(); $

我在做什么

preg_match("/\<\/p\> (\d*?) /", $error, $matches)
preg\u match(“/\(\d*?)/”,$error,$matches)
其中
$error=“bla bla bla

12345 bla 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;
$values=$form->getValue();
$url=”http://something.freshdesk.com/helpdesk/tickets.xml";
$request=“”.$values['subject']。“$username.”;
echo$请求;
$headers=array('Content-Type:application/xml','Content-Length:'.strlen($request));
变量转储($headers);
$ch=curl_init();
curl_setopt($ch,CURLOPT_USERPWD,'email@email.com:密码“);
curl_setopt($ch,CURLOPT_URL,$URL);
卷曲设置($ch,卷曲设置桩,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_头,true);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
$http\u result=curl\u exec($ch);
$error=curl\u error($ch);
$http\u code=curl\u getinfo($ch,CURLINFO\u http\u code);
卷曲关闭($ch);
如果($error){
打印“

$error”; }否则{ if(preg\u match(“/\(\d*?)/”,$error,$matches)){ $ticket_id=$matches[1]; 打印“提交的票证:$Ticket\u id\n”; } 打印“

未找到位置标题”; var_dump($matches); 变量转储($http\u结果); echo$错误; echo$http_结果;
您的正则表达式是正确的,它肯定匹配。您的问题是您没有在http响应时执行正则表达式。请使用
$http\u result
而不是
$error

preg_match("/\<\/p\> (\d*)/", $http_result, $matches);
preg\u match(“/\(\d*)/”,$http\u result,$matches);
找到了解决方案:


我应该在
$error

上使用
htmlspecialchars()
您的$error中有一个错误,应该是$error=(赋值)而不是==(比较)…这总是一个知道正则表达式是否真的错了的好工具:\%3C\%2Fp\%3E+%28\d*%3F%29%2F&subject=bla bla bla bla+%3C%2Fp%3E+12345+bla bla bla bla bla bla bla bla,在这种情况下,正如@webarto上面所说的,它是代码本身,在这种情况下是赋值已正确用于赋值,我的意思是($error==(expression))是True@JacobM:是否要从HTML字符串中提取
标记中的文本?如果是,为什么不使用Dom解析?这里的示例-@Sammaye:另外,我尝试了一个类似于您建议的工具,它工作正常:Array([0]=>

12345[1]=>12345)你不应该对
$http\u result
执行
preg\u match
而不是
$error
?@shiplu.mokadd.im也是这样。第一次使用CURL和XML以及所有这些东西,我的头都乱了
preg_match("/\<\/p\> (\d*)/", $http_result, $matches);