Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

First word count script

Featured Replies

I am trying to do the following and having some difficulty. I am trying to echo the words in the array as the key along with the total times the word appears in the string. Any advisewould be great.

 

 

 

Part 1: Write a function countWords($str) that takes any string of characters and finds the
number of times each word occurs. You should ignore the distinction between capital and
lowercase letters, and do not have to worry about dealing with characters that are not letters.
Hint: Create an associative array mapping word keys to the number of times they occur. You will
need to look at PHP's string functions to split a sentence into words.
Hint 2: The print_r($array_name) function is useful for examining the contents of an
array.

 

My code:

<?php

// my string
$str = "My name is dale and i am a programmer learning php a programmer am i dale";

// my string to lower case
$str = strtolower($str);

// split string into words and put into an array
$words = explode(' ', $str);

// counts values of array
$counts = array_count_values($words); 

foreach ($counts as $key => $value) {
	echo $value;
}

?>

 

 

 

 

 

 

 

I am trying to do the following and having some difficulty. I am trying to echo the words in the array as the key along with the total times the word appears in the string. Any advisewould be great.

 

 

 

My code:

<?php

// my string
$str = "My name is dale and i am a programmer learning php a programmer am i dale";

// my string to lower case
$str = strtolower($str);

// split string into words and put into an array
$words = explode(' ', $str);

// counts values of array
$counts = array_count_values($words); 

foreach ($counts as $key => $value) {
	echo $value;
}

?>

 

Now, explode turns your data into an array splitting the parts up by using the space as a splitter, so you don't need array counts to get the data back.

 

<?php

// my string
$str = "My name is dale and i am a programmer learning php a programmer am i dale";

// my string to lower case
$str = strtolower($str);

// split string into words and put into an array
$words = explode(' ', $str);

foreach ($words AS $key => $value) {
	echo $value.'<br />';
}


//Counts How many parts are in this array?
echo 'There are '.count($words).' words in this array!';

?>

Edited by Penguin_Media

  • Author

Brilliant so the count() function counts the total amount of words in a string. Is there a way to count how many of each words there are? For example the word 'programmer' count as twice. Is there a way to echo in a nicer format print_r($counts)?

 

$str = "My name is dale and i am a programmer learning php a programmer am i dale";
$str = strtolower($str);
$words = explode(' ', $str);
$counts = array_count_values($words); 

print_r($counts);

Edited by dpuk44

 

Brilliant so the count() function counts the total amount of words in a string. Is there a way to count how many of each words there are? For example the word 'programmer' count as twice. Is there a way to echo in a nicer format print_r($counts)?

 

$str = "My name is dale and i am a programmer learning php a programmer am i dale";
$str = strtolower($str);
$words = explode(' ', $str);
$counts = array_count_values($words); 

print_r($counts);

 

 

Add me up on skype: nitrophysix, Ill help you for free :)

 

The count is counting how many parts there are in an array.

Is this what you are looking for

<?php

// my string
$str = "My name is dale and i am a programmer learning php a programmer am i dale";

// my string to lower case
$str = strtolower($str);

// split string into words and put into an array
$words = explode(' ', $str);

$count = array();

// Loop throgh the resulys and start counting
foreach ($words as $value) {

// Check to see if the word exists if so increment the count if not start the count at 1
if (isset($count[$value])) {
$count[$value]++;
} else {
$count[$value] = 1;
}
}
// Display the results
echo "<pre>".print_r($count, true)."</pre>";

?>

Edited by D4Y0

 

Output

Array
(
    [my] => 1
    [name] => 1
    [is] => 1
    [dale] => 2
    [and] => 1
    [i] => 2
    [am] => 2
    [a] => 2
    [programmer] => 2
    [learning] => 1
    [php] => 1
)
Sorry for the double post the bb coded F'ed up the code formatting

you could use this solution

 

<?php


// my string
$str = strtolower("My name is dale and i am a programmer learning php a programmer am i dale");

// split string into words and put into an array
$words = explode(' ', $str);

$vals = array_count_values($words);


echo "<pre>";
print_r($vals);
echo "</pre>";


foreach($vals as $k => $v)
{
  echo "<b>".$k. "</b> occurs " .$v." times in this sentence <br/>";
}




?>

 

 

it is self explanatory i hope :)

 

this script will return:

 

Array
(
[my] => 1
[name] => 1
[is] => 1
[dale] => 2
[and] => 1
=> 2
[am] => 2
[a] => 2
[programmer] => 2
[learning] => 1

 => 1
)
my occurs 1 times in this sentence
name occurs 1 times in this sentence
is occurs 1 times in this sentence
dale occurs 2 times in this sentence
and occurs 1 times in this sentence
i occurs 2 times in this sentence
am occurs 2 times in this sentence
a occurs 2 times in this sentence
programmer occurs 2 times in this sentence
learning occurs 1 times in this sentence
php occurs 1 times in this sentence

Edited by inlinedivblock

  • Author

thats fantastic everyone, really appreciate it. i will study the code and put it to practice in another scenario to learn from. Thanks again

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.