【问题标题】:preg_match does not match in PHP, although the regular expression seems to be correct [closed]preg_match 在 PHP 中不匹配,尽管正则表达式似乎是正确的 [关闭]
【发布时间】:2012-08-15 11:20:14
【问题描述】:

我在做

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

在哪里$error="bla-bla-bla &lt;/p&gt; 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;

【问题讨论】:

  • 你有错误$error,应该是$error = (assignment) 而不是== (comparison)...
  • 这始终是了解正则表达式是否实际错误的好工具:switchplane.com/awesome/preg-match-regular-expression-tester/…\%3C\%2Fp\%3E+%28\d*%3F%29%2F&subject =bla-bla-bla+%3C%2Fp%3E+12345+bla-bla-bla 在这种情况下,正如@webarto 上面所说,它是代码本身,在这种情况下是分配。淘汰过程
  • webarto: 实际上$error = (assignment) 已经被正确地用于赋值了,我的意思是($error == (expression)) 是True
  • @JacobM:您希望从 HTML 字符串中提取 &lt;p&gt; 标记内的文本吗?如果是这样,为什么不使用 Dom 解析?这里的例子 - stackoverflow.com/questions/1612653/extract-text-from-tag
  • @Sammaye:另外,我已经尝试过您建议的工具,它工作正常:Array ( [0] => 12345 [1] => 12345 )

标签: php preg-match


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    找到解决方案:

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

    【讨论】:

    • 你不应该对$http_result而不是$error执行preg_match吗?
    • @shiplu.mokadd.im 是的,也是。第一次使用 CURL 和 XML 以及所有这些东西,搞砸了我的头
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2018-07-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多