Re: twodimensional array / word-frequencylist

From: Date: Thu, 26 Jun 2003 11:44:38 +0000
Subject: Re: twodimensional array / word-frequencylist
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-152957@lists.php.net to get a copy of this message
There may be faster ways, for instance using the regular expression function that returns an array of hits, and make the regexp look for 'words' in it with the \b option. But i will stick to your example code. My suggestions are not tested.
while($myrow = mysql_fetch_row($result)) # as long as there are texts in the database {
        $text = $myrow[0]; # variabele text contains the text
        $array = array(); # variabele array is an empty array
        $modText = strtolower($text); #modify text: string to lower case
//to also split on other signs than only the space $modText=str_replace('.',' ',$modText); $modText=str_replace("\t",' ',$modText); //tab $modText=str_replace("\n",' ',$modText); //newline // add whatever you want
        $array = split(" ",$modText); #split text after empty space to create array
of words
$words=array(); for ($word = 0; $word < count($array); $word++) { if (!isset($words[$word]
        $words[$word]=0;
else
        $words[$word]++;
} sort($words); for ($word = 0; $word < count($words); $word++) { echo 'The word '. $word.' appears '.$words[$word] .' times !<br> }
# i would like a similar function that removes interpuntuation like "." etc. # all i want remaining in the array are the separate words, all in lower case
        sort($array); # sort array elements in alphabetical order
        $num = count($array); # count the number of elements in the array
        for ($c=0; $c<=$num; $c++) #as long as function has nog reached past last
element in array
NOTE that you are trying to go through the count from 0 to and including count. This is not good. $array($num) does not exist! So make that
        for ($c=0; $c<$num; $c++)
//
        {
                $wordInText = $array[$c]; #array element is word in text
                echo "$wordInText<br>"; #print word in text
        }
+++++++++++++++++++++++++++++++++++
N    Dore van Hoorn
S    s1000454 AlfaInformatica-RuG
E    M.D.van.Hoorn@student.rug.nl
W    http://www.let.rug.nl/~s1000454
+++++++++++++++++++++++++++++++++++ werk je voor de uni?


« previous php.general (#152957) next »