montymoose
Posts: 85
|
| Posted: 11/29/2007, 4:16 PM |
|
Hello CodeChargers!
Using PHP and MYSQL can anyone think of a way of taking a string and then returning a list detailing how many times each word has been used in the string?
Eg.
INPUT STRING = "the cat is called Frank. Frank is the best cat in the world"
RESULT
the 3
cat 2
frank 2
is 2
world 1
best 1
called 1
in 1
Any ideas how to get to the result... to be honest I'm not too sure where to start.
Cheers,
M00SE
|
 |
 |
datadoit
|
| Posted: 11/29/2007, 7:43 PM |
|
$text = explode(' ',"the cat is called Frank. Frank is the best cat in
the world");
foreach ($text as $key => $word ) {
if (strlen($word) > 1) {
$words[] = $word;
}
}
$count = array_count_values($words);
array_multisort($count,SORT_DESC);
$i = 1;
foreach ($count as $key => $value) {
if( $i <= 20 ) {
echo 'Number of occurances:<b> '.$value.' </b>of word:
<b>'.$key."</b><br>\n";
$i++;
} else {
exit();
}
}
|
|
|
 |
montymoose
Posts: 85
|
| Posted: 11/30/2007, 2:19 AM |
|
Thanks so much man!
That worked a treat... the only problem is that the word "Frank" is only counted once as a fullstop turns the second "Frank" into a different work - "Frank."
However I'm sure I can fix that with a str_replace on the initial string.
Thanks again for your help.
M00s3
|
 |
 |
montymoose
Posts: 85
|
| Posted: 11/30/2007, 2:49 AM |
|
<?php
$string = "This is some text and numbers 12345 and symbols !£$%^&";
$new_string = ereg_replace("[^A-Za-z0-9]", "", $string);
echo $new_string
?>
This works to remove question marks and commas etc... however it seems to take out all the whitespaces as well creating only one long word. I'm sure it shouldn't ... any thoughts?
|
 |
 |
DonB
|
| Posted: 11/30/2007, 5:23 AM |
|
Try this:
$string = "the cat is called Frank. Frank is the best cat in the world";
var_dump( concordance($string) );
function concordance($string) {
$words = array();
foreach (explode(" ",$string) as $word) {
$word = ereg_replace("[^A-Za-z0-9]", "", strtolower($word));
if (!array_key_exists($word, $words))
$words[$word] = 1;
else
$words[$word]++;
}
return $words;
}
--
DonB
"montymoose" <montymoose@forum.codecharge> wrote in message
news:5474f5661792be@news.codecharge.com...
> Hello CodeChargers!
>
> Using PHP and MYSQL can anyone think of a way of taking a string and then
> returning a list detailing how many times each word has been used in the
> string?
>
> Eg.
>
> INPUT STRING = "the cat is called Frank. Frank is the best cat in the
world"
>
> RESULT
>
> the 3
> cat 2
> frank 2
> is 2
> world 1
> best 1
> called 1
> in 1
>
> Any ideas how to get to the result... to be honest I'm not too sure where
to
> start.
>
> Cheers,
>
> M00SE
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>
|
|
|
 |
lvalverdeb
Posts: 299
|
| Posted: 11/30/2007, 1:02 PM |
|
A shorter way:
$string = "the cat is called Frank. Frank is the best cat in the world";
$words = array_count_values(str_word_count(strtolower(ereg_replace("[^A-Za-z0-9]", " ", $string)),1));
print_r($words);
Luis
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4 |
 |
 |
montymoose
Posts: 85
|
| Posted: 11/30/2007, 1:35 PM |
|
Cheers guys - both sets of code work great.
|
 |
 |