doumie7914 2015-09-27 18:46
浏览 70
已采纳

查找每个单词出现在php中的字符串中的次数

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);
?>
  • 写回答

2条回答 默认 最新

  • douhuan4699 2015-09-27 19:17
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配