我需要能够计算出一个特定的单词在一个特定的html标签中出现的次数。目前,我只能计算出现在标签中的单词总数。我可以计算单词在文档中出现的总次数,但是我不知道如何计算单词在h3标记中出现的次数。
我需要的示例:
Sample text here, blah blah blah, lorem ipsum
<h3>Lorem is in this h3 tag, lorem.</h3>
lorem ipsum dolor....
<h3>This is another h2 with lorem in it</h3>
正如您所看到的,单词"lorem“在代码中出现了4次,但我只想计算单词"lorem”在h3标记中出现的次数。
我更喜欢在这个项目上继续使用PHP。
非常感谢您的帮助
发布于 2012-09-02 01:33:59
您也可以使用正则表达式来执行此操作:
<?php
$string = 'Sample text here, blah blah blah, lorem ipsum
<h3>Lorem is in this h3 tag, lorem.</h3>
lorem ipsum dolor....
<h3>This is another h2 with lorem in it</h3>';
preg_match_all("/lorem(?=(?:.(?!<h3>))*<\/h3>)/i", $string, $matches);
if (isset($matches[0])) {
$count = count($matches[0]);
} else {
$count = 0;
}
?>
发布于 2012-09-02 01:26:32
我会这样使用DOMDocument:
$string = 'Sample text here, blah blah blah, lorem ipsum
<h3>Lorem is in this h3 tag, lorem.</h3>
lorem ipsum dolor....
<h3>This is another h2 with lorem in it</h3>';
$html = new DOMDocument(); // create new DOMDocument
$html->loadHTML($string); // load HTML string
$cnt = array(); // create empty array for words count
foreach($html->getElementsByTagName('h3') as $one){ // loop in each h3
$words = str_word_count(strip_tags($one->nodeValue), 1, '0..9'); // count words including numbers
foreach($words as $wo){ // create an key for every word
if(!isset($cnt[$wo])){ $cnt[$wo] = 0; } // create key if it doesn't exit add 0 as word count
$cnt[$wo]++; // increment it's value each time it's repeated - this will result in the word having count 1 on first loop
}
}
var_export($cnt); // dump words and how many it repeated
https://stackoverflow.com/questions/12229766
复制相似问题