【发布时间】:2012-08-15 11:20:14
【问题描述】:
我在做
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;
【问题讨论】:
-
你有错误$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 字符串中提取
<p>标记内的文本吗?如果是这样,为什么不使用 Dom 解析?这里的例子 - stackoverflow.com/questions/1612653/extract-text-from-tag -
@Sammaye:另外,我已经尝试过您建议的工具,它工作正常:Array ( [0] => 12345 [1] => 12345 )
标签: php preg-match