Number of times that word apears in text
Moderator: General Moderators
Number of times that word apears in text
How to count number of times that single word is repeated in block of text
I need to make function, but dont know where to start from.
$x=some_number
$text=block_of_text_to_search_words_in_it
function givetop($x, $text)
{
}
If somebody could point me in right direction I would aprecheate it
Regards
ddragas
I need to make function, but dont know where to start from.
$x=some_number
$text=block_of_text_to_search_words_in_it
function givetop($x, $text)
{
}
If somebody could point me in right direction I would aprecheate it
Regards
ddragas
How to sort array to get top X words desc?
Here is some code until now
Here is some code until now
Code: Select all
$some_text = "You’ve worked really hard, your product is at last finished, so why on earth would you give it away? Well because it works – here’s why.
The internet grew up on the premise of giving stuff away. No one has ever expected to pay to view information on an internet web site. This has resulted in most internet businesses having to devise complicated business models to make a living. Here’s the most popular one. “Give and you shall receive.”
Surfers on the web love getting something for nothing. They won’t buy immediately from you, so don’t disappoint them and give them what they want. Still don’t see the point?";
$No = 5;
function gettop_x_words($x, $text)
{
$words = explode(" ", $text);
foreach ($words as $key => $value)
{
if(strlen($value) > 3)
{
$count_words = substr_count ($text, $value);
$array_counted_words[] = array(array('word' => $value, 'count'=> $count_words));
}
}
$get_words = print_r($array_counted_words);
return $get_words;
}
echo gettop_x_words($No, $some_text);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
That's something different that what I was talking about and what I thought you were asking about.
Roughly, I would strip out all non-alphanumerics (after the explosion) and then run array_count_values() then sort it.
Roughly, I would strip out all non-alphanumerics (after the explosion) and then run array_count_values() then sort it.
I can not see result.
Why?
Why?
Code: Select all
function gettop_x_words($x, $text)
{
$words = explode(" ", $text);
foreach ($words as $key => $value)
{
if(strlen($value) > 3)
{
$count_words = substr_count ($text, $value);
$array_counted_words[] = array(array('word' => $value, 'count'=> $count_words));
}
}
rsort($array_counted_words);
foreach ($array_counted_words as $word)
{
echo "$word[0] $word[1]<br>\n";
}
}I'm guessing here because your description is not too good.
Code: Select all
function get_keywords($word_string, $num_words_to_return = 10, $noise_words = array(), $min_word_length = 3)
{
preg_match_all('/\w{'.$min_word_length.',}/', strtolower($word_string), $matches);
if(empty($matches[0])) return array();
foreach($matches[0] as $word) in_array($word, $noise_words) or @$occurrences[$word]++;
arsort($occurrences);
$keys = array_keys($occurrences);
$values = array_values($occurrences);
for($i = 0, $j = count($keys); $i < $j and $i < $num_words_to_return; $i++)
{
$return[] = array('word' => $keys[$i], 'count' => $values[$i]);
}
return $return;
}