简体   繁体   中英

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:

echo count($playerPicks);

You can use simple Javascript - which is done on the clients machine, so it'll save server resources.

JSFiddle

var str = document.body.innerText;
var n = str.match(/Score/ig);
  • The i flag means case insensitive search
  • The g flag means to do a global match (match all occurrences, not just one)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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