【问题标题】:Find how many times each word appeared in string in php查找每个单词在php中的字符串中出现的次数
【发布时间】:2015-09-27 18:46:41
【问题描述】:

我有一个带有注释框文本区域的 HTML 表单。我希望能够计算输入了多少单词(我已经用str_word_count 完成了,然后我希望能够告诉用户每个单词在字符串中出现了多少次。我可以打印像这样的值@987654326 @ 但是我将如何输出到显示单词和计数的 2 列表中?

感谢您的帮助!

表格代码:

<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);
?>

【问题讨论】:

标签: php


【解决方案1】:

您在 cmets 中发布的代码还可以,但它会将不同大小写的单词视为不同的单词(如“Comments”和“cmets”)。所以不要忘记使用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>

此脚本回显:

How many words were input: 3

Word     Count
comments     2
i            1
like         1

【讨论】:

  • 谢谢!这正是我一直在寻找的。我唯一的另一个问题是,我如何让每一行都有不同的背景颜色?
  • 很高兴它成功了!您可以使用 cssbackground-color 属性对其进行样式设置。
  • 有没有办法在 php 文件上做内联 css?当我将类添加到表标签时出现错误
  • 应该使用评论部分来澄清有关答案的内容,如果您有其他问题,请随时打开一个新的,以防您在本网站或网络上找不到它。话虽如此,我不建议使用内联样式,你真的应该keep it separated from PHP/HTML
  • 如果计数值为 10 则首先显示,如果另一个值为 9 则显示在下方,如何按计数链接显示结果顺序
【解决方案2】:

要显示在两列中,只需循环遍历数组。你会得到你的结果

<?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

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2016-06-28
    相关资源
    最近更新 更多