我想浏览一个网页并计算每个DIV中的单词数量。我该怎么做呢?我开始使用PHP DOMDocument。我想知道这是否是最好的方法。可以就如何做到这一点使用一些建议。我目前的代码是:
$dom = new DOMDocument;
$dom->loadHTML( $contents );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div");
foreach ( $pDivs as $div ) {
// some code to count the text
}