Using preg_match and relevant comments, please fill in the blanks _____ of the f
ID: 3717161 • Letter: U
Question
Using preg_match and relevant comments, please fill in the blanks _____ of the following PHP code:
<!DOCTYPE html>
<!-- Project3.php
Uses a function to determine the three most frequently occurring words in the given string, where the words are delimited on the left by spaces and on the right by commas, periods, or question marks
-->
<html lang = "en">
<head>
<title> Project3</title>
<meta charset = "utf-8" />
</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 words that occur most often
// in the given array
function the_three($in_str) {
// Split the string into its words
preg_match_all("/ +[A-Za-z]+[,.?]/u", $in_str, $word_list);
_________ ($word_list[0] as $word)
print("The words are: $word <br />");
// Create the empty word frequency array
$freq = array();
// Loop to count the words (either increment or initialize to 1)
foreach ($word_list[0] as $word) {
// First, get rid of the delimiters
______________("/[A-Za-z]+/", $word, $match);
$word = $match[0];
// Now, get an array of the keys
$keys = array_keys($freq);
// Set the frequency for the work
if(in_array($word, $keys))
$freq[$word]++;
else
$freq[$word] = ________;
}
// Sort the frequency array in reverse order of values
arsort($freq);
// Get the keys and return the first three
$new_keys = array_keys($freq);
return array($new_keys[0], $new_keys[1], $new_keys[2]);
} #** End of the_three
// Main test driver
$test_str = " 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_str);
// Display the words and their frequencies
print "<br /> The Three Most Frequently Occurring Words<br /><br />";
$sorted_keys = array_keys($tbl);
sort($sorted_keys);
____________ ($sorted_keys as $word)
print "$tbl[$word] <br />";
?>
</body>
</html>
Explanation / Answer
<!DOCTYPE html>
<!-- Project3.php
Uses a function to determine the three most frequently occurring words in the given string, where the words are delimited on the left by spaces and on the right by commas, periods, or question marks
-->
<html lang = "en">
<head>
<title> Project3</title>
<meta charset = "utf-8" />
</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 words that occur most often
// in the given array
function the_three($in_str) {
// Split the string into its words
preg_match_all("/ +[A-Za-z]+[,.?]/u", $in_str, $word_list);
foreach($word_list[0] as $word)
print("The words are: $word <br />");
// Create the empty word frequency array
$freq = array();
// Loop to count the words (either increment or initialize to 1)
foreach ($word_list[0] as $word) {
// First, get rid of the delimiters
preg_match_all("/[A-Za-z]+/", $word, $match);
$match = $match[0];
// Now, get an array of the keys
$keys = array_keys($freq);
// Set the frequency for the work
if(in_array($word, $keys))
$freq[$word]++;
else
$freq[$word] =1;
}
// Sort the frequency array in reverse order of values
arsort($freq);
// Get the keys and return the first three
$new_keys = array_keys($freq);
return array($new_keys[0], $new_keys[1], $new_keys[2]);
} #** End of the_three
// Main test driver
$test_str = " 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_str);
// Display the words and their frequencies
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 />";
?>
</body>
</html>
Output:
The words are: apples.
The words are: are,
The words are: good?
The words are: for,
The words are: you,
The words are: or,
The words are: you,
The words are: like?
The words are: apples,
The words are: or.
The words are: maybe.
The words are: you,
The words are: like,
The words are: better.
The words are: than.
The words are: apples?
The Three Most Frequently Occurring Words
you,
apples.
are,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.