-2

我在做

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;
4

2 回答 2

1

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

preg_match("/\<\/p\> (\d*)/", $http_result, $matches);
于 2012-08-15T11:35:44.150 回答
0

找到了解决方案:

我应该用htmlspecialchars()$error

于 2012-08-15T12:13:18.310 回答