简体   繁体   English

计算页面上特定单词的出现

[英]Count specific word occurrence on page

I have a webpage that has a table of usernames and scores, and it changes weekly based on scores. 我有一个网页,其中包含用户名和分数表,并且每周都会根据分数进行更改。 I just want to have a number of user's shown on my page. 我只想在页面上显示多个用户。 My table has multiple 's so using a script to count those will not work. 我的表有多个,因此使用脚本来计算它们将不起作用。

Every row does have something in common though, the word "Score". 但是,每一行都有一些共同点,即“分数”。 So I figured I could just count that word, and return the value. 所以我想我可以数那个单词,然后返回值。 I cannot figure out how to just count that one word after the page is loaded completely. 我无法弄清楚如何在页面完全加载后只计算一个单词。

Help? 救命?

Edit: fixed tag 编辑:固定标签

Well that was easy, sorry for the bother. 嗯,这很容易,麻烦您了。

I have $playerPicks listed in the table, so all I did was use: 我在表中列出了$playerPicks ,所以我所做的就是使用:

echo count($playerPicks);

You can use simple Javascript - which is done on the clients machine, so it'll save server resources. 您可以使用简单的Javascript-在客户端计算机上完成,这样可以节省服务器资源。

JSFiddle JSFiddle

var str = document.body.innerText;
var n = str.match(/Score/ig);
  • The i flag means case insensitive search i标志表示不区分大小写的搜索
  • The g flag means to do a global match (match all occurrences, not just one) g标志意味着进行全局匹配(匹配所有匹配项,而不仅仅是匹配一次)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM