首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统计特定单词在h3标记中出现的次数

统计特定单词在h3标记中出现的次数
EN

Stack Overflow用户
提问于 2012-09-02 01:13:46
回答 2查看 770关注 0票数 1

我需要能够计算出一个特定的单词在一个特定的html标签中出现的次数。目前,我只能计算出现在标签中的单词总数。我可以计算单词在文档中出现的总次数,但是我不知道如何计算单词在h3标记中出现的次数。

我需要的示例:

代码语言:javascript
运行
复制
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。

非常感谢您的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-02 01:33:59

您也可以使用正则表达式来执行此操作:

代码语言:javascript
运行
复制
<?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;
     }

?>
票数 0
EN

Stack Overflow用户

发布于 2012-09-02 01:26:32

我会这样使用DOMDocument

代码语言:javascript
运行
复制
$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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12229766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档