我希望此代码检查元素是否存在,然后获取div中的记录数。
html
<div id=myDiv>This table contains 5 records (3 seconds)</div>
php
$selector = new Zend_Dom_Query($response->getBody());
$nodes = $selector->query('#myDiv');
if (count($nodes) < 1) {
$this->setValue(0);
return;
}
$el = $nodes->current();
if (preg_match('#('d+['d,]*)'s+result#i', $el->nodeValue, $matches)) {
$number = str_replace(',', '', $matches[1]);
$this->setValue(intval($number));
} else {
Zend_Registry::get('log')->err(get_class($this).': Unable to match response and regex.');
$this->setValue(0);
}
现在它到达底部$this->setValue(0),因此它可以找到元素。正则表达式一定有问题,这就是我一无所知的地方。
if (preg_match('#('d+['d,]*)'s+result#i', $el->nodeValue, $matches)) {
HTML代码不包含任何与#('d+['d,]*)'s+result#i
匹配的内容,表达式要求存在字符串"result"。
你可能想把这个词改成"记录"。例如:
/('d+)'s+record/i