Jump to content

random


shage

Recommended Posts

why do you think that???

you could do like:

 

<?php

  $verb = array('walked','ran','skipped','jogged');
  $adj = array('orange','red','blue','yellow');
  $noun = array('computer','sign','door','wall');
  echo "I ". $verb[rand(0,3)] . " to the market and saw a " . $adj[rand(0,3)] . " toy car so i stole it and threw it at the " . $noun[rand(0,3)];

?>

 

always an interesting outcome :D

Link to comment
Share on other sites

And... to take that a step further.

 

<?php

  function parse_replace($str) {
    $verb = array('walked','ran','skipped','jogged');
    $adj = array('orange','red','blue','yellow');
    $noun = array('computer','sign','door','wall');
    $str = str_replace(
      array('[verb]','[adj]','[noun]'),
      array($verb[rand(0,count($verb)-1)],$adj[rand(0,count($adj)-1)],$noun[rand(0,count($noun)-1)]),
      $str
    );
    return $str;
  }

  echo parse_replace("I [verb] to the market and saw a [adj] toy car so i stole it and threw it at the [noun]");

?>

 

Depends what your doing really.

Link to comment
Share on other sites

guys are getting there and looking good, but i cant figure out how to set it up to use the concept of (red,blue,green,orange) in the text and it random one of them, is how its done but cant seem to figure it out.

Link to comment
Share on other sites

guys are getting there and looking good, but i cant figure out how to set it up to use the concept of (red,blue,green,orange) in the text and it random one of them, is how its done but cant seem to figure it out.

 

You would need to use regular expressions (www.php.net/ereg) to grab all the text data in between parans, than from there filter each of those into an array and do the above code on them to get the one word, put each word in an array and use either str_replace to replace the entire (word,word) with the new word.

 

This takes into account that the text will never have parans as part of the actual text.

 

Shouldn't be too hard to do.

Link to comment
Share on other sites

if i had the code i wouldnt need help, lol

 

The idea that (i,we) have is to have it pick a random word from within the (). This is to allow a story to change depending on every (load, refresh). The thing is (i,we) can't seem to figure out how to random whats inside the () as it will change from story to story. Later we hope to have it where the kids can enter their own parts of the story, showing action words etc, kind of a fun game.

Link to comment
Share on other sites

From the examples weve posted this should be easy. I'm sorry, but were not going to write it for you. If you don't understand it from here then I see little hope. Not to be harsh.... but I just can't see how we could explain things any clearer.

Link to comment
Share on other sites

ok look i will explain exactly what you should need:

 

for the first part the () words:

$words = array('word1','word2','word3');

 

ok now for the random part:

$selection = $words[rand(0,count($words)-1)];

 

tada now just take $selection and use it...

Link to comment
Share on other sites

<?php
$text = "The (quick,slow) brown fox (hopped,jumped,leaped) over the (stubborn,stingy,nice) fence!";
if (eregi("\(([A-Z0-9,-_]*)\)", $text, $regs)) { // edited here to add the comma and hyphen and underscore.
       foreach ($regs as $match) {
            // unsure if the paran portion is included so lets replace them.
            $new_text = str_replace("\(", "", $match);
            $new_text = str_replace(")", "", $new_text); // this may need escaping the ( \) )
            $words = explode(",", $new_text);
            $new_text = "(" . $new_text . ")"; // add them back for replacing later.
            $selection = $words[rand(0,count($words)-1)];
            $text = str_replace($new_text, $selection, $text);
       }

      echo $text;
}
?>

 

That is the gist of it. I just coded it on the fly, no testing I am sure the regular expression is wrong, but for help with that look at http://www.zend.com/zend/spotlight/code-gallery-wade5.php and  http://www.regular-expressions.info/characters.html

 

Have fun!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.