Link to home
Start Free TrialLog in
Avatar of mcainc
mcainc

asked on

PHP string randomization with Regular Expressions?

Hey guys,

I'm a little confused at how to approach this particular function. What I have is a string like this:

{Hello|Hi|Welcome|Good evening} thanks for {stopping by|joining us|coming by} please enjoy your stay!

Open in new window


What I'm looking for is a function that can randomly pick an element from within the { } but delimited by the |.

Example output would be:

Hi thanks for joining us please enjoy your stay!
Good evening thanks for stopping by please enjoy your stay!
Welcome thanks for coming by please enjoy your stay!
Hello thanks for joining us please enjoy your stay!

Hopefully this makes sense, can someone give me a hand?
Avatar of rinfo
rinfo

Why not define two arrays for
$var1 = array (Hello ,Hi,Welcome,Good evening);
$var2 = array (stopping by,joining us,coming by);
then using random function pick two values between 0-3 and 0-2
$ele1 = rand(0,3);
$ele2 = rand(0.2);
now your string would be
$greet = $var1[$ele1].'  thanks for  '.$var2[$ele2] .'  please enjoy your stay!';
sorry array will be
$var1 = array ('Hello' ,'Hi,Welcome','Good evening');
$var2 = array ('stopping by','joining us','coming by');
I have attached a dynamic function.
$stringtemplate = '{Hello|Hi|Welcome|Good evening} thanks for {stopping by|joining us|coming by} please enjoy your stay!';

echo string_selector($stringtemplate, 2, 1) . "<br />\n";
echo string_selector($stringtemplate) . "<br />\n";

Open in new window

exex-27656711.php
ASKER CERTIFIED SOLUTION
Avatar of animecyc
animecyc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcainc

ASKER

Wow thanks guys this is perfect!