title: Find how many times each word appeared in string in php
body: <p>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 <code>str_word_count</code> 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 <code>Array ( [I] =&gt; 1 [like] =&gt; 1 [comments] =&gt; 1 )</code> but how would I output into a 2 column table where it shows the word and the count?</p>
  
  <p>Thanks for any help!</p>
  
  <p>Form Code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;PHP Form&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;form name="newForm" method="post" action="formProcess.php"&gt;UserName:
      &lt;input type="text" name="userName" size="15" maxlength="15"&gt;
      &lt;br&gt;Password:
      &lt;input type="password" name="pass1" size="15"&gt;
      &lt;br&gt;Confirm Password:
      &lt;input type="password" name="pass2" size="15"&gt;
      &lt;br&gt;
      &lt;p&gt;I agree to the terms and conditions.
        &lt;br&gt;
        &lt;input type="radio" name="terms" value="yes"&gt;Yes
        &lt;input type="radio" name="terms" value="no"&gt;No
        &lt;p&gt;Enter comments here:
          &lt;br&gt;
          &lt;textarea name="comments" rows="6" cols="50" wrap="physical"&gt;&lt;/textarea&gt;
          &lt;p&gt;
            &lt;input type="submit" name="submitForm"&gt;
            &lt;input type="reset" name="resetForm"&gt;
          &lt;/p&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  
  $userName = $_POST[userName];
  $pass1 = $_POST[pass1];
  $pass2 = $_POST[pass2];
  $terms = $_POST[terms];
  $comments = $_POST[comments];
  
  echo "Username: $userName&lt;br /&gt;";
  echo "Terms Agreed to? $terms&lt;br /&gt;";
  echo "Your comments: $comments&lt;br /&gt;";
  
  $count = str_word_count($_POST['comments']);
  
  print_r( array_count_values(str_word_count($comments, 1)) );
  
  echo "Total words in comment box: $count&lt;br /&gt;";
  
  function validatePassword($pass1,$pass2) { 
      if($pass1 === $pass2) 
          {         
            $msg = "Password confirmed!"; 
          } 
          else 
          {
            $msg = "Passwords do not match!"; 
          } 
          return $msg;
  }
  echo validatePassword($pass1, $pass2);
  ?&gt;
  </code></pre>