检查domnodelist-> item(x) - > nodeValue ==“nbsp;” [英] Check if domnodelist->item(x)->nodeValue == "nbsp;"

查看:170
本文介绍了检查domnodelist-> item(x) - > nodeValue ==“nbsp;”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经登录了,并使用 CURL 抓住了返回页面,加载了 DOMDocument 然后查询它与 DOMXPATH (以找到'table.essgrid tr')。 (然后我还查询结果找到孩子的td和)结果, results-> item(2) - > nodeValue 是一个日期还是什么在浏览器中回显为& nbsp; 。我需要检查它是否是一个非休息空间或实际文本。

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>';
            }
        }
    }
}

所以我想要这样做只能回应table.essgrid> tr> td,它的值不是一个不间断的空间,但只是回到页面上:

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>

但它正在回应所有结果,而不仅仅是时间的结果。所以我认为我的问题是检查值== & nbsp; ,但没有任何我尝试的地方似乎有效。

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.

推荐答案

当您想比较 nodeValue & nbsp; ,你需要知道两件事:

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


  1. & nbsp; 是一个表示特定字符​​的HTML实体,这里可以正式指定 char / a0 / index.htmrel =nofollow> Unicode字符'NO-BREAK SPACE'(U + 00A0)

  2. DOM文档 library在给或接受字符串值时使用UTF-8作为字符编码。

  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.

有了这个一般信息,很容易解决你的问题由于& nbsp; 代表 NO-BREAK SPACE(U + 00A0) DOMElement :: nodeValue 返回内容为UTF-8编码的字符串,而UTF-8中的 NO-BREAK SPACE \xC2\xA0在PHP中,您可以简单地比较:

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.

这篇关于检查domnodelist-&gt; item(x) - &gt; nodeValue ==“nbsp;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆