Php:字符串索引不一致?

3
我创建了一个函数,它可以从硬编码的单词列表中随机生成短语。我有一个名为get_words()的函数,它有一串硬编码的单词字符串,将其转换为数组,然后进行洗牌并返回。 generate_random_phrase()调用get_words(),它迭代get_words() n次,并在每次迭代中将n个单词连接成最终短语,该短语将返回给用户。
我的问题是,由于某种原因,PHP给我提供的结果不一致。它确实给了我随机化的单词,但是单词数量不一致。我将默认值设置为4个单词,但它给出的短语单词数从1到4不等,而不是4个。这个程序非常简单,几乎令人难以置信我无法确定确切的问题所在。似乎链条中断裂的地方是正在被索引的$words数组,由于某种原因,有时索引会失败。我不熟悉PHP,有人能解释一下吗?
<?php

function generate_random_phrase() {
  $words = get_words();
  $number_of_words = get_word_count();
  $phrase = "";
  $symbols = "!@#$%^&*()";
  echo print_r($phrase);

  for ($i = 0;$i < $number_of_words;$i++) {
  $phrase .= " ".$words[$i];
}


  if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

  if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

  return $phrase;
}

function get_word_count() {
  if ($_POST['word_count'] < 1 || $_POST['word_count'] > 9) 
    $word_count = 4; #default
  else
    $word_count = $_POST['word_count'];
  return $word_count;
}

function get_words() {
  $BASE_WORDS = "my sentence really hope you 
    like narwhales bacon at midnight but only 
    ferver where can paper laptops spoon door knobs 
    head phones watches barbeque not say";
  $words = explode(' ', $BASE_WORDS);
  shuffle($words);
  return $words;
}
?>
3个回答

2

你的函数似乎有点不一致,因为你也在数组中包含空格,所以当你将它们包含在循环中时,似乎5个单词(4个实际单词和一个空格索引)并不正确。你可以先过滤空格,包括空格。

这是我所说的可视化表示:

Array
(
    [0] =>             // hello im a whitespace, i should not be in here since im not really a word

    [1] => but
    [2] => 
    [3] => bacon
    [4] => spoon
    [5] => head
    [6] => barbeque
    [7] => 
    [8] => 
    [9] => sentence
    [10] => door
    [11] => you
    [12] => 
    [13] => watches
    [14] => really
    [15] => midnight
    [16] => 

所以当你循环时,包括这种情况下的空格。如果你有一个单词数为5,你实际上并没有得到那5个单词,索引0-4看起来像只有3个(1 => but, 3 => bacon, 4 => spoon)。

这是修改后的版本:

function generate_random_phrase() {
    $words = get_words();
    $number_of_words = get_word_count();
    $phrase = "";
    $symbols = "!@#$%^&*()";
    $words = array_filter(array_map('trim', $words)); // filter empty words
    $phrase = implode(' ', array_slice($words, 0, $number_of_words)); // no need for a loop
    // this simply gets the array from the first until the desired number of words (0,5 or 0,9 whatever)
    // and then implode, just glues all the words with space
    // so this ensure its always according to how many words you want

    if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

    if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

    return $phrase;
}

2
在$BASE_WORDS中,制表符和换行符占据了数组中的空间,这就是为什么会出现问题。删除换行符和制表符,程序将生成正确的答案。例如:
$BASE_WORDS = "my sentence really hope you like narwhales bacon at midnight but only ferver where can paper laptops spoon door knobs head phones watches barbeque not say";

哇,你真是个天才!谢谢。 - chopper draw lion4

0

你的单词列表中间的空格不一致是问题所在。

这里有一个解决方法:

function get_words() {
  $BASE_WORDS = "my|sentence|really|hope|you|
|like|narwhales|bacon|at|midnight|but|only|
|ferver|where|can|paper|laptops|spoon|door|knobs|
|head|phones|watches|barbeque|not|say";
  $words = explode('|', $BASE_WORDS);
  shuffle($words);
  return $words;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接