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

查找每个单词在php字符串中出现的次数,php,Php,我有一个带有注释框文本区的HTML表单。我希望能够计算输入的单词数(我已经使用str\u word\u count完成了这一操作,然后我希望能够告诉用户每个单词在字符串中出现了多少次。我可以打印如下数组([I]=>1[like]=>1[comments]=>1)的值但是如何将数据输出到一个2列的表中,在该表中显示单词和计数 谢谢你的帮助 表格编号: <html> <head> <title>PHP Form</title> </head&

我有一个带有注释框文本区的HTML表单。我希望能够计算输入的单词数(我已经使用
str\u word\u count
完成了这一操作,然后我希望能够告诉用户每个单词在字符串中出现了多少次。我可以打印如下
数组([I]=>1[like]=>1[comments]=>1)的值
但是如何将数据输出到一个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:


要在两列中显示,只需在数组中循环。您将得到结果

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

要在两列中显示,只需在数组中循环。您将得到结果

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

您在评论中发布的代码是可以的,但它将不同大小写的单词视为不同的单词(如“comments”和“comments”)。因此不要忘记使用
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>

您在评论中发布的代码是可以的,但它将不同大小写的单词视为不同的单词(如“comments”和“comments”)。因此不要忘记使用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>

你检查了吗?int substr_计数(字符串$haystack,字符串$pinder[,int$offset=0[,int$length]]))可能重复的不理解下一票,链接是关于字符的。我对php还不太了解。什么是haystack和needle?现在我的textarea发布到一个
$comments
变量。这与哪一个是等价的?在@SubinThomas comment之后,看一看:,示例你检查了这个吗?int substr\u count(字符串$haystack,字符串$needle[,int$offset=0[,int$length]])可能重复的不理解下一票,链接是关于字符的。我对php还不太了解。什么是haystack和needle?现在我的textarea发布到一个
$comments
变量。哪一个是等效的?在@SubinThomas comment之后,看一看:,示例,谢谢!这正是我想要的我在寻找。我唯一的另一个问题是,如何使每一行具有不同的bg颜色?很高兴它工作了!您可以使用
背景色
属性对其进行样式设置。是否有方法在php文件上执行内联css?当我将类添加到表标记时,我遇到了一个错误。注释部分应用于澄清某些内容关于答案,如果你有其他问题,请随时打开一个新的,以防你在本网站或网络上找不到。尽管如此,我不建议使用内联样式,你真的应该这样做。如果count值为10,则如何按count链接显示结果顺序?如果count值为10,则首先显示,如果另一个值为9,则在下面显示谢谢!这是正确的y我一直在寻找。我唯一的另一个问题是,如何使每一行具有不同的bg颜色?很高兴它工作了!您可以使用
背景色
属性来设置样式。是否有方法在php文件上执行内联css?当我将类添加到表标记时,我遇到了一个错误。应该使用注释部分来澄清关于答案,如果你有其他问题,请随时打开一个新的,以防你在本网站或网站上找不到。尽管如此,我不建议使用内联样式,你真的应该这样做。如果count值为10,则如何按计数链接显示结果顺序?如果count值为10,则显示第一个,如果另一个值为9,则显示第二个