word_table.php

来自「对于这种语言是比较的麻烦的了..你自己慢慢体会了..好难说清楚的了」· PHP 代码 · 共 54 行

PHP
54
字号
<<html >
<head> <title> word_table.php </title>
</head>
<body>
<?php

// Function splitter
//  Parameter: a string of text containing words and punctuation
//  Returns: an array in which the unique words of the string are
//           the keys and their frequencies are the values.

function splitter($str) {

// Create the empty word frequency array

  $freq = array();

// Split the parameter string into words

  $words = preg_split("/[ .,;:!?]\s*/", $str);

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

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

  return $freq;
} #** End of splitter

// Main test driver

  $str = "apples are good for you, or don’t you like apples? 
or maybe you like oranges better than apples";

// Call splitter

  $tbl = splitter($str);

// Display the words and their frequencies

  print "<br /> Word Frequency <br /><br />";
  $sorted_keys = array_keys($tbl);
  sort($sorted_keys);
  foreach ($sorted_keys as $word)
    print "$word $tbl[$word] <br />";
?>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?