I have had a look on Google, and on the PHP.net site but couldnt find what I was looking for.
I need a script that counts how many of a certain words there are in a variable. For instance:
How many times the is said in this phrase: " I went to the seaside, and the sea was out. ". So that would return 2.
Anybody have any ideas?
Thanks
[SOLVED] Counting words in a string
Moderator: General Moderators
[SOLVED] Counting words in a string
Last edited by Archy on Tue Feb 01, 2005 12:43 pm, edited 1 time in total.
I dont how I missed that the first time :sIf you just want to find out how many times a string contains another simple string, don't use preg_match_all like I did before I fould the substr_count function.
Use
<?php
$nrMatches = substr_count ('foobarbar', 'bar');
?>
instead. Hope this helps some other people like me who like to think too complicated
Thanks.