在php中从字典中生成随机单词

发布于 2024-12-22 21:11:27 字数 228 浏览 0 评论 0原文

请查看此链接:来自字典的随机条目

该示例是 C#< /code>,但是,我的问题是如何从 php 的字典中生成随机条目?

php 是否有内置的字典函数,或者我需要 API 或其他东西来执行此操作?

Please take a look at this link: Random entry from dictionary

That example is C#, however, my question is how to generate a random entry from the dictionary in php?

Does php have an inbuilt function for dictionaries, or will I need an API or something else to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻许诺言 2024-12-29 21:11:27

从 GitHub,您可以下载包含每行中所有字典单词的文本文件。完整的指南在这里 - 获取PHP 教程中从英语词典中随机单词

下面是随机选择一行并获取该行单词的 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;
?>

来源:从 PHP 英语词典中获取随机单词

From GitHub, you can download the text file that contains all the dictionary word in each line. The complete guide is here - Get random word from English dictionary in PHP tutorial

Below is the PHP code to choose a line randomly and get the word of that line:

<?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;
?>

Source: Get random word from English dictionary in PHP

星星的轨迹 2024-12-29 21:11:27

如果我正确理解了这个问题, array_rand 可能会做你想做的事情。

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

array_rand might do what you want, if I understand the question correctly.

$array = array(
    'key' => 'value',
    'key2' => 'value2',
    // etc
);
$randomKey = array_rand($array); // get a random key
echo $array[$randomKey]; // get the corresponding value
寂寞清仓 2024-12-29 21:11:27

为了完成汤姆的回答,我在我的一个项目中也有类似的需求,这里我的类有一个棘手的功能,即字典直接嵌入到 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

Just to complete Tom answer, I had similar needs in one of my project, here my class the tricky feature the dictionary is directly embedded in the PHP file.

/**
 * 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文