У меня есть HTML-форма с текстовым полем комментариев. Я хочу быть в состоянии подсчитать, сколько слов было введено (что я сделал с str_word_count
, тогда я хочу сказать пользователю, сколько раз каждое слово появлялось в строке. Я могу напечатать значения, подобные этому Array ([I] => 1 [like] => 1 [comments] => 1)
, но как бы Я вывод в таблицу 2 столбца, где он показывает слово и подсчетНайти, сколько раз каждое слово появилось в строке в php
Спасибо за любую помощь
формы Код:!
<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);
?>
http://stackoverflow.com/questions/32420848/count-the-occurrences-of-all-the- letter-in-a-string-php/32420890 # 32420890 Вы это проверили? – user5173426
int substr_count (string $ haystack, string $ needle [, int $ offset = 0 [, int $ length]]) –
Возможный дубликат [Считать частоту слова в тексте?] (Http://stackoverflow.com/questions/ 2984786/php-sort-and-count-instance-of-words-in-a-string-string) – Federkun