PHP DOM,在特定div中查找文本 [英] PHP DOM, Find the text within a certain div

查看:59
本文介绍了PHP DOM,在特定div中查找文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在不使用正则表达式的情况下为游戏开发开发者跟踪器.尝试解析某些部分的html时遇到障碍.

I am currently working a developer tracker for a game without using regex. I have hit a road block when trying to parse html at certain parts.

我要解析的内容:

<td class="alt1" id="td_post_139718"> 
<!-- message, attachments, sig --> 
        <!-- icon and title --> 
        <div class="smallfont"> 
            <img class="inlineimg" src="images/icons/icon1.gif" alt="Default" border="0" /> 
            <strong>Re: TERA's E3 2010 Coverage</strong> 
        </div> 

我的代码:

$titleArray = array();
        foreach($idArray as $id) {
            $title = $dom->getElementById('td_post_'.$id);
            $smallFont = $title->getElementsByTagName("div");
            echo $smallFont->nodeValue;
        }

它产生:

Notice: Undefined property: DOMNodeList::$nodeValue in C:\wamp\www\crawler\crawler.php on line 71

Notice: Undefined property: DOMNodeList::$nodeValue in C:\wamp\www\crawler\crawler.php on line 71

Notice: Undefined property: DOMNodeList::$nodeValue in C:\wamp\www\crawler\crawler.php on line 71

我正在尝试在动态范围内的文本中查找文本.

I am trying to find the text within a that is within a dynamic .

我已经尝试了各种组合来尝试使它起作用,但是我已经能够实现它.

I've tried all sorts of combinations to try and get it to work but I've been able to achieve it.

推荐答案

:: getElementsByTagName 给出了一个节点列表.您必须遍历它以检索单个< div> .示例:

The ::getElementsByTagName gives a node list. You have to iterate through it to retrieve the individual <div>s. Example:

foreach ($title->getElementsByTagName("div") as $smallFont)) {
    echo htmlspecialchars($smallFont->nodeValue), "<br />;
}

您还可以改为使用 textContent 属性.参见例如此讨论.

You can also use the textContent property instead. See e.g. this discussion.

这篇关于PHP DOM,在特定div中查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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