计算来自用户输入PHP

时间:2018-11-18 03:23:18

标签: php

我一直把头撞在墙上,试图找出答案。有人可以帮我吗我不知道自己在做什么错。

这应该接受用户输入,计算单词的频率,并显示列表中使用频率最高的三个单词。当我运行它时,所有打印的内容都是我键入的内容。

输出应该看起来像这样:

三个最常见的单词

苹果

<!DOCTYPE html>

<!--    Project3.php
        Uses a function to determine the three most frequently
        occurring strings in a given array of strings and returns
        them in an array.
    -->
<html lang = "en">
<head> 
<title> Project3.php </title>
</head>
<body>
<?php

// Function the_three
//  Parameter: a string containing words that are delimited
//             on the left by spaces and on the right by
//             commas, periods, or question marks
//  Returns: an array of the three strings that occur most often
//           in the given array

function the_three($in_array) {

// Create the empty word frequency array

  $freq = array();
  preg_match_all('/([a-z]+)(?=/W|$)/i', $test_array, $matches[0]);

// Loop to count the words (either increment or initialize to 1)

  foreach ($in_array as $word) {
    $keys = array_keys($freq);
    if(in_array($word, $keys))
      $freq[$word]++;
    else
      $freq[$word] = 1;
  }

  arsort($freq);
  $new_keys = array_keys($freq);
  return array($new_keys[0], $new_keys[1], $new_keys[2]);
} #** End of the_three

// Main test driver

  $test_array = array($_POST['words']);

//array("apples", "are", "good", "for", "you", "or", 
//   "don't", "you", "like", "apples", "or", "maybe", "you", "like",
//   "oranges", "better", "than", "apples");

// Call the function

  $tbl = the_three($test_array);

// Display the words and their frequencies

if (isset($_POST['words'])) {

  print "<br /> The Three Most Frequently Occurring Words<br /><br />";
  $sorted_keys = array_keys($tbl);
  sort($sorted_keys);
  foreach ($sorted_keys as $word)
    print "$tbl[$word] <br />";

}
?>

<br>
Enter Sentences: <br>
<form method = "POST">
<input type="text" name="words"/>
<input type="submit" name="Submit" />

</body>
</html>

3 个答案:

答案 0 :(得分:0)

单词之间的分隔符是''空格还是?

$words = explode(' ', $_POST['words']);
$wordsFrequency = array_count_values($words);

答案 1 :(得分:0)

您可以使用array_count_values大大简化代码。请注意,您需要使用explodepreg_split$_POST的值转换为数组。如果所有单词都用空格分隔,则explode最简单:

$_POST['words'] = "apples are good for you or don't you like apples or maybe you like oranges better than apples";
// convert to an array
$test_array = explode(' ', $_POST['words']);
// get word counts
$counts = array_count_values($test_array);
// sort counts in descending order
arsort($counts);
// and get the 3 most common words
$sorted = array_keys($counts);
print "<br /> The Three Most Frequently Occurring Words<br /><br />\n";
for ($i = 0; $i < 3; $i++) {
    echo $sorted[$i] . "<br/>\n";
}

输出

<br /> The Three Most Frequently Occurring Words<br /><br />
apples<br/> 
you<br/> 
or<br/>

Demo on 3v4l.org

答案 2 :(得分:0)

第一:

// Will not give you an array of words
// It will give you 1 array element containing all your words.
$test_array = array($_POST['words']);

执行以下操作:

$test_array = preg_split('/ +/, $_POST['words']);

使用preg_split,以便它可以处理一个或多个空格。

然后我将简化功能:

function the_three($in_array) {
    // Create the empty word frequency array
    $freq = [];
    // Loop to count the words (either increment or initialize to 1)

    foreach ($in_array as $word) {
        if( array_key_exists($word, $freq) )
            $freq[$word]++;
        else
            $freq[$word] = 1;
    }

    arsort($freq);
    // This should return an array with the three highest
    // frequency words with the number of times they occurred.
    return array_slice($freq, 0, 3);
}