raimis100 Posted January 16, 2009 Share Posted January 16, 2009 Hey! I need to get some things done. I guess I will need to use regex. I have a string (great,cool) site, I (like,enjoyed) it What I should do is make it grab one of the words in brackets It can randomly be Great site, I like it Cool site, I like it etc. Any suggestion how could I do this ? Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/ Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 <?php $adjectives = array('great', 'cool'); $verbs = array('like', 'enjoyed'); $string = $adjectives[rand(0, count($ajectives)-1)] . " site, I " . $verbs[rand(0, count($verbs)-1)] . " it"; ?> Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738437 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 <pre> <?php $data = <<<DATA Great site, I like it Cool site, I like it DATA; print_r(preg_split('/site,\s+I\s+|\s+it(?:\s+|\z)/', $data, -1, PREG_SPLIT_NO_EMPTY)); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738440 Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 Wow, I completely missed the question. ??? Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738442 Share on other sites More sharing options...
raimis100 Posted January 16, 2009 Author Share Posted January 16, 2009 Well ... This was not what I was acutally looking for. But I cannot make it work that way. User don't enter arrays and place where it should be randomly picked User just enters text (great,cool) site, I (like,enjoyed) it Okey, lets try to make it up by pieces. First I need to scrape every word in brackers and then randomly choose one word from it. After that brackets and content it in should be replaced with that one randomly choosen word Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738445 Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 Is there not a better way to get the input from the user? Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738450 Share on other sites More sharing options...
raimis100 Posted January 16, 2009 Author Share Posted January 16, 2009 Nop Since I do not know regex I will try to put it together using substr and strpos functions Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738452 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 <pre> <?php $data = <<<DATA (great,cool) site, I (like,enjoyed) it i love (apples,oranges,bananas,grapes,tangerines,cake,pie) DATA; echo preg_replace_callback( '/\((.*?)\)/', create_function( '$words', '$words = split(",", $words[1]); return array_rand(array_flip($words));' ), $data ); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738455 Share on other sites More sharing options...
raimis100 Posted January 16, 2009 Author Share Posted January 16, 2009 Works great effigy! Thanks you! Where is the thanks or +rep button ? Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738464 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 You've already taken care of the thanks part. Clicking the "TOPIC SOLVED" button in the bottom left is all that's left. Link to comment https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/#findComment-738481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.