shage Posted July 5, 2007 Share Posted July 5, 2007 Anyone point me in the right direction for a way to have something like I (walked,ran,skipped,jogged) to the market The script would select one word in the () and use it. Thank you Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 <?php $words = array('walked','ran','skipped','jogged'); echo "I ". $words[rand(0,3)] . " to the market"; ?> Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 lol nice thorpe this is kinda hmmmm simple Quote Link to comment Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 thats nice but would be a pain to use if i was to write a story and change one word per paragraph, thank you thou Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 You asked for an example, there it is. It would be pretty easy to build upon. Quote Link to comment Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 k u the man Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 Maybe if you explain exactly what your trying to do / post some code. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 thorpe you are genius man thats pretty great work Quote Link to comment Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 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... Quote Link to comment Share on other sites More sharing options...
per1os Posted July 5, 2007 Share Posted July 5, 2007 <?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! Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 that would be a good way too... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.