【问题标题】:Generate random word from the dictionary in php在php中从字典中生成随机单词
【发布时间】:2011-12-24 13:23:39
【问题描述】:

请查看此链接:Random entry from dictionary

那个例子是C#,但是,我的问题是如何在php中从字典中生成一个随机条目?

php 是否有内置的字典功能,或者我需要一个 API 或其他东西来做到这一点?

【问题讨论】:

    标签: php random dictionary word


    【解决方案1】:

    从 GitHub,您可以download 包含每行中所有字典单词的文本文件。完整指南在这里 - Get random word from English dictionary in PHP tutorial

    下面是随机选择一行并获取该行单词的PHP代码:

    <?php
    $file = "words_alpha.txt";
    $file_arr = file($file);
    $num_lines = count($file_arr);
    $last_arr_index = $num_lines - 1;
    $rand_index = rand(0, $last_arr_index);
    $rand_text = $file_arr[$rand_index];
    echo $rand_text;
    ?>
    

    来源:Get random word from English dictionary in PHP

    【讨论】:

      【解决方案2】:

      array_rand 可能会做你想做的事,如果我理解正确的话。

      $array = array(
          'key' => 'value',
          'key2' => 'value2',
          // etc
      );
      $randomKey = array_rand($array); // get a random key
      echo $array[$randomKey]; // get the corresponding value
      

      【讨论】:

      • @Foobert:您可以在网络上随处获得免费的字典文本文件
      • @RageZ 我认为他的意思是“关联数组”中的字典,而不是词汇表。
      • @TomvanderWoerdt:添加链接不好?
      • @RageZ 我没看到。 :-)
      【解决方案3】:

      为了完成汤姆的回答,我在我的一个项目中也有类似的需求,在这里我的班级将字典直接嵌入到 PHP 文件中的棘手功能。

      /**
       * Generate bunch of english Lorem Ipsum.
       * 
       * 
       * 
       * @category Tool
       * @package Tool
       * @version 
       */
      class RandomWord
      {
          /**
           * List of words in the dictionnary 
           * 
           * @var array
           */
          static private $_words = array();
      
          /**
           * Load the dictionary. 
           * 
           * This would load the dictionnary embedded in this PHP file
           */
          static public function loadDictionnary()
          {
              $fp = fopen(__FILE__, 'r');
              $halted = false;
      
              while (($line = fgets($fp, 4096)) !== false) {
                  if ($halted == false) {
                      if (preg_match('/__halt_compiler\(\);/', $line)) {
                          $halted = true;
                      }    
                  } else {
                      list($word, $dummy) = explode("\t", $line);
                      array_push(self::$_words, $word);    
                  }
              }
      
              fclose($fp);
          }
      
          /**
           * Pickup a random word from the dictionnary
           * 
           * @return string
           */
          static public function random()
          {
              if (!count(self::$_words)) {
                  self::loadDictionnary();
              }
              return self::$_words[array_rand(self::$_words)];
          }
      
          /**
           * Generate a number of paragraph of random workds 
           * 
           * @param integer $numberOfWords
           * @param integer $paragraph
           * @return string
           */
          static public function loremIpsum($numberOfWords = 20, $paragraph = 1)
          {
              $ret = '';
              for ($i = 0; $i < $paragraph; $i++) {
                  for ($v = 0; $v < $numberOfWords; $v++) {
                      $ret .= self::random() . ' ';
                  }
                  if ($paragraph > 1) {
                      $ret .= "\n";
                  }
              }
      
              $ret = substr($ret, 0, strlen($ret) - 1);
      
              return $ret;
          }
      }
      
      __halt_compiler();
      's gravenhage   |h
      'tween  |v
      'tween decks    |v
      .22 |N
      0   |NA
      1   |NA
      1-dodecanol |N
      1-hitter    |N
      10  |NA
      100 |NA
      1000    |NA
      10000   |N
      100000  |N
      1000000 |N
      1000000000  |N
      1000000000000   |N
      1000th  |A
      100th   |A
      10th    |A
      11  |NA
      11-plus |N
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-17
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        相关资源
        最近更新 更多