Check if domnodelist->item(x)->nodeValue == “nbsp;”

谁都会走 提交于 2019-12-24 02:33:08

问题


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>&nbsp;</div>
<div>not blank:</div>
<div>&nbsp;</div>
<div>not blank:</div>
<div>13:00</div>
<div>not blank:</div>
<div>&nbsp;</div>
<div>not blank:</div>
<div>14:30</div>
<div>not blank:</div>
<div>13:00</div>
<div>not blank:</div>
<div>&nbsp;</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 == &nbsp;, but nothing I have tried in its place seems to work.


回答1:


When you want to compare nodeValue for being &nbsp;, you need to know two things:

  1. &nbsp; 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).
  2. 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 &nbsp; 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 "&nbsp;"
}

Hope this gives you the needed pointers.



来源:https://stackoverflow.com/questions/26617586/check-if-domnodelist-itemx-nodevalue-nbsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!