Php: String indexing is unstable?

I created a function that randomly generates a phrase from a hard-coded list of words. I have a function get_words()

that has a string of hardcoded words that turns into an array, then shuffles and returns.

get_words()

is called generate_random_phrase()

, which get_words()

iterates through n times, and at each iteration concatenates n words into a final phrase that is meant to be returned to the user.

My problem is that PHP keeps giving me inconsistent results for some reason. This gives me words that are randomized, but gives an inconsistent word count. I specify 4 words as the default, and this gives me phrases ranging from 1-4 words instead of 4. This program is so simple that it is almost unbelievable that I cannot pinpoint the exact question. It seems that the broken link in the chain is an array $words

that is being indexed, it seems for some reason the indexing fails sometimes. I am unfamiliar with PHP, can someone explain this to me?

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


source to share


3 answers


In $ BASE_WORDS, your tabs and newlines are taking up space in the exploded array, why. Remove newlines and tabs and it will generate the correct answer. I.e:



$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";

      

+2


source


Your function seems a bit inconsistent since you also include spaces inside the array, so when you included them, you include them in your loop, which seems to be 5 words (4 real words with one space index) is actually correct. At first you can just filter for spaces, including spaces.

Here's a visual representation of what I mean:

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] => 

      



So when you loop it, you include spaces in this case. If you have multiple words 5

, you don't really get those 5 words, index 0 - 4

it will look like you only got 3 ( 1 => but, 3 => bacon, 4 => spoon

).

Here's a modified version:

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


source


Immediate spacing in a wordlist is a problem.

Here's the fix:

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

      

0


source







All Articles