0

I am new to php and trying out some different things. I got a problem with printing a random value from a string from multiply values.

    $list = "the weather is beautiful tonight".
    $random = one random value from $list, for example "beautiful" or "is"

Is there any simple way to get this done? Thanks!

1
  • 2
    the function you want is explode() Commented Dec 10, 2015 at 23:01

2 Answers 2

1

well, as @Dagon suggested, you can use explode() to get an array of strings, then you can use rand($min, $max) to get an integer between 0 and the length of your array - 1. and then read the string value inside your array at the randomly generated number position.

Sign up to request clarification or add additional context in comments.

1 Comment

Dagon maybe will tell you taht you have missed to explain how to get the length of the array;)
0
// First, split the string on spaces to get individual words
$arg = explode(' ',"the weather is beautiful tonight");

// Shuffle the order of the words
shuffle($arg);

// Display the first word of the shuffled array
print $arg[0];

2 Comments

he would learn more if you diffident just proved the whole answer
If i where him:i have tested the code, see it worked, then i take a look at all function at php.net or elsewhere, because i want too learn. "Is there any simple way to get this done?" Was the question and i have an answer. I dont have asked: Can you teach me too archive that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.