【发布时间】: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);
?>
【问题讨论】:
-
int substr_count (string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
-
不理解downvote,链接是关于字符的。我是php新手,还不太了解。什么是干草堆和针?现在我的 textarea 发布到
$comments变量。这相当于哪一个? -
跟随@SubinThomas 评论,看看:php.net/manual/en/function.substr-count.php,例如w3schools.com/php/func_string_substr_count.asp
标签: php