0

I have an HTML form with a comment box textarea. I want to be able to count how many words were input (which I have done with str_word_count then I want to be able to tell the user how many times each word appeared in the string. I can print the values like this Array ( [I] => 1 [like] => 1 [comments] => 1 ) but how would I output into a 2 column table where it shows the word and the count?

Thanks for any help!

Form Code:

<html>
<head>
  <title>PHP Form</title>
</head>

<body>
  <form name="newForm" method="post" action="formProcess.php">UserName:
    <input type="text" name="userName" size="15" maxlength="15">
    <br>Password:
    <input type="password" name="pass1" size="15">
    <br>Confirm Password:
    <input type="password" name="pass2" size="15">
    <br>
    <p>I agree to the terms and conditions.
      <br>
      <input type="radio" name="terms" value="yes">Yes
      <input type="radio" name="terms" value="no">No
      <p>Enter comments here:
        <br>
        <textarea name="comments" rows="6" cols="50" wrap="physical"></textarea>
        <p>
          <input type="submit" name="submitForm">
          <input type="reset" name="resetForm">
        </p>
  </form>
</body>

</html>

PHP:

<?php

$userName = $_POST[userName];
$pass1 = $_POST[pass1];
$pass2 = $_POST[pass2];
$terms = $_POST[terms];
$comments = $_POST[comments];

echo "Username: $userName<br />";
echo "Terms Agreed to? $terms<br />";
echo "Your comments: $comments<br />";

$count = str_word_count($_POST['comments']);

print_r( array_count_values(str_word_count($comments, 1)) );

echo "Total words in comment box: $count<br />";

function validatePassword($pass1,$pass2) { 
    if($pass1 === $pass2) 
        {         
          $msg = "Password confirmed!"; 
        } 
        else 
        {
          $msg = "Passwords do not match!"; 
        } 
        return $msg;
}
echo validatePassword($pass1, $pass2);
?>
still2blue
  • 193
  • 1
  • 18
  • http://stackoverflow.com/questions/32420848/count-the-occurrences-of-all-the-letters-in-a-string-php/32420890#32420890 did you check this? – DirtyBit Sep 27 '15 at 18:48
  • 2
    int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) – Subin Thomas Sep 27 '15 at 18:48
  • possible duplicate of [Count word frequency in a text?](http://stackoverflow.com/questions/2984786/php-sort-and-count-instances-of-words-in-a-given-string) – Federkun Sep 27 '15 at 18:50
  • Don't understand the downvote, the link is about chars. I am new to php and still not quite understanding. What is haystack and needle? Right now my textarea posts to a `$comments` variable. Which one is that equivelant to? – still2blue Sep 27 '15 at 18:50
  • Following @SubinThomas comment, take a look at: http://php.net/manual/en/function.substr-count.php , example http://www.w3schools.com/php/func_string_substr_count.asp – Pedro Lobito Sep 27 '15 at 18:52
  • Okay so `print_r( array_count_values(str_word_count($comments, 1)) );` prints out the values, but how would I get it to output the words and counts in a table? – still2blue Sep 27 '15 at 18:54
  • @still2blue: we are blank when you don't show us a code.. Post your code, then you get better support. – Subin Thomas Sep 27 '15 at 18:55
  • To iterate it use [foreach](http://php.net/manual/it/control-structures.foreach.php). – Federkun Sep 27 '15 at 18:55
  • @SubinThomas post edited. – still2blue Sep 27 '15 at 18:59
  • Your function `validatePassword` scares me ! – Pedro Lobito Sep 27 '15 at 19:07

2 Answers2

3

The code you posted in the comments is ok, but it considers words written with different casing as different words (like "Comments" and "comments"). So don't forget to use strtolower:

<?php  
$comments = "Comments? I like comments.";

$commentsArray = array_count_values(str_word_count(strtolower($comments), 1));

echo "<p>How many words were input: " . count($commentsArray) . "</p>";
?>
<table>
    <tr>
        <th>Word</th>
        <th>Count</th>
    </tr>
    <?php foreach($commentsArray as $word=>$count): ?>
    <tr>
        <td><?php echo $word; ?></td>
        <td><?php echo $count; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

This script echoes:

How many words were input: 3

Word     Count
comments     2
i            1
like         1
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
  • Thank you! This is exactly what I was looking for. Only other question I have, is how would I make each row have a different bg color? – still2blue Sep 27 '15 at 19:50
  • Glad it worked! You can style it using [css](http://www.w3schools.com/css/css_table.asp), with the `background-color` property. – Marcos Dimitrio Sep 27 '15 at 19:53
  • Is there a way to do inline css on a php file? I am getting an error when I had the class to the table tag – still2blue Sep 27 '15 at 19:55
  • The comment section should be used to clarify something about the answer, if you have other questions, feel free to open a new one in case you can't find it on this site or on the web. With that said, I don't recommend using inline styles, you really should [keep it separated from PHP/HTML](http://programmers.stackexchange.com/questions/271294/why-is-it-or-was-it-important-to-separate-css-from-html#271338). – Marcos Dimitrio Sep 27 '15 at 20:03
  • how to show the result order by count link if count value is 10 its show first and if another value is 9 its show under – Bilawal Awan Dec 01 '19 at 11:27
1

to show in two column, just loop through the array. and you'll get your result

<?php

$string = "Hello, still2blue. This is your string. This string is repeated";

$words_list = str_word_count($string, 1); // this returns the array of words 

$results = array_count_values($words_list);
foreach($results as $word => $count){
    echo sprintf("%-10s %2d", $word, $count) . PHP_EOL;
}

Example code on Ideone

ssi-anik
  • 2,998
  • 3
  • 23
  • 52