2014-10-05 59 views
3

我已經創建了一個函數,它從硬編碼的單詞列表中隨機生成一個短語。我有一個功能get_words()它有一個硬編碼字的字符串,它變成一個數組,然後洗牌並返回。Php:字符串索引不一致?

get_words()被稱爲generate_random_phrase(),它通過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 = "[email protected]#$%^&*()"; 
    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; 
} 
?> 

回答

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"; 
+0

哇,你是個天才!謝謝 – 2014-10-05 02:02:10

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個詞,index 0 - 4它看起來像只有3個(1 => but, 3 => bacon, 4 => spoon)。

下面是修改後的版本:

function generate_random_phrase() { 
    $words = get_words(); 
    $number_of_words = get_word_count(); 
    $phrase = ""; 
    $symbols = "[email protected]#$%^&*()"; 
    $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; 
} 
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; 
}