Recent Topics

1 Jul 03, 2006 13:10    

Given a text file with 10 strings that could be anything, why is there not a simple way to count how many times each string occurs??? Ive read every php site on the net, looked at every string related function. Nothing nada nilch, everything I have read requires you to know what $needle you are looking for.

What if the strings are subject to change?

2 Jul 03, 2006 16:03

Do you know the length of the strings? Or is there something that separates them? You could read the file, then explode it into an array and try searching for each string in the array.

3 Jul 03, 2006 16:05

Um... how can you search for something if you don't know what it is? In other words, if you have no value for $needle how would you know if you found it or not?

4 Jul 03, 2006 20:03

answer to first questions, sure :) there is a space seperating each item

apple orange pineapple pear kiwi lemon apple apple kiwi lemon

and im not really trying to search, least i dont think. I just want to count how many apples, how many kiwis, etc.. but some days there might be a whole nother list of fruit, like no kiwis but instead watermelon :)

hence the problem with the php $needle in $haystack idea :(

5 Jul 03, 2006 20:24

Is this what ur looking for.

Here you go (see attached)

7 Jul 03, 2006 20:44

Dam those regular expressions ;) ( need to learn them sometime :D )

Here's the modified code from personman's suggestion to do what you want. It returns the same output as method_one of the attached file in my previous post.

$str = 'apple orange pineapple pear kiwi lemon apple apple kiwi lemon';

echo 'Personman\'s suggestion';
function MostPopularWords($target, $minimum_word_length = 3)
    preg_match_all('/[a-z]{'.$minimum_word_length.',}/', strtolower($target), $matches);
    $words = array();
    foreach($matches[0] as $word)
        isset($words[$word]) ? $words[$word]++ : $words[$word] = 1 ;
    array_multisort($words, SORT_NUMERIC, SORT_DESC);
	return $words;
echo '<pre>';
echo '</pre>';

8 Jul 03, 2006 20:44

mmm :) thanks both of you!!!

thats pretty code, balupton, unfortunately, its doing that $needle in $haystack thing where you have to define the needles, (wont work) the needles might change. :(

personman, that might be 'zactly what I am looking for.. :) good to know im not the only lost soul on the 'net looking for something like that :P

9 Jul 03, 2006 20:53

MethodTwo and MethodThree of my code doesn't return an array with keys...
[ array ( $key => $value ) ]....

But yeh, otherwise just use a split on your string, or the modified code from personman's suggestion that i attached previously...

Thats if i'm following what you mean...

Form is loading...