Jump to content
xisto Community
Sign in to follow this  
Eggie

Exact Word Counting

Recommended Posts

I need a script or something...
i need to count how many times a word like "love" or "car" is used in a website ot in text without having to count it one by one...
is there any script or addon for mozilla that does it?
i need answer fast...
i googled it but came up with nothing

if noone knows...can you just write a script which count the number of a word "car" in

$text=my car is the most beautiful car in the world;

Edited by Eggie (see edit history)

Share this post


Link to post
Share on other sites

I have no clue if there is a better (more secure way of doing this) but you could just run a str check. Now I am just making up this code so I have no clue it works. Basically what I am going to do is check the length of the string before and after the word is replaced. Subtract the two (gives you the number of letters total) then I check the length of the replaced word and divide that by the result above. I like using basic concepts to get the job done.

$text="my car is the most beautiful car in the world";$word=("car");$normal_length=strlen($text);$replaced_length=strlen(str_ireplace($word,"",$text));$result_length=($normal_length-$replaced_length);$word_length=strlen($word);$count=($result_length/$word);
Now I did this pretty much step by step (you can make it a LOT shorter). Here is my explaination:
Steps:
1) Set the text var with text you want to search
2) Set the word to search for.
3) Find length of normal string and string without the word.
4) Subtract to find the difference in characters.
5) Check the length of the word.
6) Do some more basic math to find the number of times the word is used and return it as $count.

Is this what you were looking for? Hope this helps,
Sparkx

Share this post


Link to post
Share on other sites

I like sparkx's way of doing it, but one thing you might want to do is make $word " car " (with spaces around it). This stops words like "carousel" showing up as one of the words. Admittedly, you'd have to set $word_length to strlen($word)-2 to take the spaces in to account, but that shouldn't be too bad.One problem with this approach is that punctuation can ruin the count, as " car." wouldn't count, nor would "Car " (as in if it's at the beginning of the string). Capital letters need to be taken in to account as well. Anyone got any ideas on how to improve on this?

Share this post


Link to post
Share on other sites

You could use regex, like "/\wcar\w/i", I think \w is the opposite of \W, which is alphanumeric, then whatever your languages version of regml(0) is.

Share this post


Link to post
Share on other sites

Mordent I like your addition (I can't believe I would have over looked that). Anyway if you do it that way you would need to set $text=" ".$text." "; so that if car is used at the beginning or end it would also show up. Capital letters are taken into account however. Notice that I used str_ireplace() instead of str_replace() which makes it case in-sensitive so basically when you replace car it will also replace Car, CAr and any cap/lowercase combo. If you use Toby's way I think the opposite of /W is [^a-zA-Z] but either way works.Thanks, Sparkx

Share this post


Link to post
Share on other sites

There's still the problem of not catching words that either start the string (and therefore don't have a space at the front) or end a sentence (and so have a "." instead of a space) or have some other piece of punctuation either before or after them. Given that we don't have any problems with capitalisation (I'll admit that I didn't notice you'd used str_ireplace() ), any thoughts on how to fix this other problem? I'm thinking using regular expressions to check for any punctuation or a space (both before and after), but I haven't used enough of them to know if this method would work. Thoughts?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.