在php中通过textcontent获取元素id

时间:2014-02-08 11:48:18

标签: php text get element

我有一个PHP代码:

$url = "http://www.bbc.co.uk/";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument();
$doc->validateOnParse = true;
@$doc->loadHtml($data);

//I want to get element id and all i know is that the element is containg text "Business"

echo $doc->getElementById($id)->textContent;

让我们假设,页面上有一个想要跟踪的元素。我不知道id,只是当时的文本内容。我希望获得id,以便我可以在下周或月份获得相同元素的textcontent,无论文本内容是否正在发生变化......

1 个答案:

答案 0 :(得分:1)

看看这个项目: http://code.google.com/p/phpquery/

有了这个,您可以使用CSS3选择器,如“div:contains('foo')”来查找包含文本的元素。

更新:示例

任务:在“test.html”中找到包含“find me”的元素:

<html>
    <head></head>
    <body>
        <div>hello</div>
        <div>find me!</div>
        <div>and find me!</div>
        <div>another one</div>
    </body>
</html>

PHP-Skript:

<?php

include "phpQuery-onefile.php";

phpQuery::newDocumentFileXHTML('test.html');
$domNodes = pq('div:contains("find me")');

foreach($domNodes as $domNode) {
    /** @var DOMNode */
    echo $domNode->textContent . PHP_EOL;
}

运行它的结果:

php test.php
find me!
and find me!
相关问题