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
https://forums.phpfreaks.com/topic/58542-random/#findComment-290372
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
https://forums.phpfreaks.com/topic/58542-random/#findComment-290387
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
https://forums.phpfreaks.com/topic/58542-random/#findComment-290397
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
https://forums.phpfreaks.com/topic/58542-random/#findComment-290402
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
https://forums.phpfreaks.com/topic/58542-random/#findComment-290428
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.