-2

Write a small program to calculate all the word frequency in the any given link, and print the result with frequency sort by alphabet.

For example, a link has an article like this: "what makes a developer bad, what makes a them good," the result should be: a 1 bad 1 developer 1 good 1 makes 2 them 1 what 2

New to php and at a roadblock. What i have so far:

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name" value="<?php echo $name;?>">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>

  <input type="submit" name="submit" value="Submit">  
</form>


<?php
// define variables and set to empty values
$nameErr = "";
$name = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


?>

<?php
echo("$name");
?> 
  • What problem are you having with it? You can use `explode()` to split the input into an array of words. You can use `array_count_values()` to count the number of repetitions of each word in an array. – Barmar Nov 13 '19 at 23:43
  • 1
    The code you've posted doesn't seem to make any attempt to solve the problem, all it does is print the `name` input. What does that have to do with the question? – Barmar Nov 13 '19 at 23:43

1 Answers1

-1

This works

test_input("Are you going to go to the store?");
function test_input($data) {
    $data = trim($data);
    $data = preg_replace("/[^a-zA-Z0-9 ]+/", "", $data);
    $process = explode(" ",$data);
    $process2 = array_count_values($process);
    foreach ($process as $live){
        $data = $live." ".$process2[$live]." ";
    }
    return $data;
}
Seth B
  • 1,014
  • 7
  • 21