问题
I have logged in to, and grabbed the return page using CURL, loaded it with DOMDocument and then queried it with DOMXPATH (to find 'table.essgrid tr'). (I am then also querying the result to find child 'td's and) with the results, results->item(2)->nodeValue is either a date or what echos in browser as or . I need to check if it will be a non break space or actual text.
Hopefully that makes some sense with the code below.
$dom = new DOMDocument();
$dom->loadHTML($result);
$xpath = new DOMXPATH($dom);
$result = $xpath->query('//table[@class="essgrid"]//tr');
if($result->length > 0) {
foreach($result as $item) {
$tds = $item->getElementsByTagName('td');
if($tds->length) {
if($tds->item(2)->nodeValue != " " && $tds->item(2)->nodeValue != " ") {
echo = '<div>not blank:</div>';
echo = '<div>'.$tds->item(2)->nodeValue.'</div>';
}
}
}
}
So I am wanting this to only echo the "table.essgrid>tr>td" that have a value that isnt a non-breaking space, but it just echos this onto the page:
<div>not blank:</div>
<div> </div>
<div>not blank:</div>
<div> </div>
<div>not blank:</div>
<div>13:00</div>
<div>not blank:</div>
<div> </div>
<div>not blank:</div>
<div>14:30</div>
<div>not blank:</div>
<div>13:00</div>
<div>not blank:</div>
<div> </div>
But it is echoing all the results, not just the ones with a time. So I think my problem is checking if the value == , but nothing I have tried in its place seems to work.
回答1:
When you want to compare nodeValue for being , you need to know two things:
is a HTML entity that represents a specific character, here the non-breaking space which could be formally specified as Unicode Character 'NO-BREAK SPACE' (U+00A0).- The DOMDocument library uses UTF-8 as character encoding when giving or accepting string values.
With this general information at hand, it's easy to solve your problem. As stands for NO-BREAK SPACE (U+00A0) and as DOMElement::nodeValue returns the contents as UTF-8 encoded string and as NO-BREAK SPACE in UTF-8 is "\xC2\xA0" in PHP you can simply compare it:
/** @var $td DOMElement */
$td = $tds->item(2);
if ($td->nodeValue !== "\xC2\xA0") {
// TD content is not " "
}
Hope this gives you the needed pointers.
来源:https://stackoverflow.com/questions/26617586/check-if-domnodelist-itemx-nodevalue-nbsp