Solved

PHP string randomization with Regular Expressions?

Posted on 2012-03-30
6
339 Views
Last Modified: 2012-03-31
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?
0
Comment
Question by:mcainc
6 Comments
 
LVL 9

Expert Comment

by:rinfo
ID: 37790700
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!';
0
 
LVL 9

Expert Comment

by:rinfo
ID: 37790702
sorry array will be
$var1 = array ('Hello' ,'Hi,Welcome','Good evening');
$var2 = array ('stopping by','joining us','coming by');
0
 
LVL 10

Expert Comment

by:acbxyz
ID: 37790767
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
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 3

Accepted Solution

by:
animecyc earned 250 total points
ID: 37791687
Here is an elegant way to do what you want in PHP5:

function formatStringFromTemplate($string) {
	if(preg_match_all('/\{.+?\}/', $string, $matches)) {
		$string = str_replace($matches[0], array_map(function($set, $index) { 
			$set = trim($set);
			$set = substr($set, 1, strlen($set) - 2);
			$set = array_map('trim', explode('|', $set));
			return $set[$index > -1 ? $index : array_rand($set)];
		}, $matches[0], array_splice(func_get_args(), 1)), $string);
	}

	return $string;
}

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

echo formatStringFromTemplate($string, 1) . '<br />';
echo formatStringFromTemplate($string, 3, 1) . '<br />';
echo formatStringFromTemplate($string);

Open in new window


I also included the above comments ability to use a specific index on each of the sets.
0
 
LVL 10

Assisted Solution

by:acbxyz
acbxyz earned 250 total points
ID: 37791766
Two remarks on that:
- array_splice throws a warning because the function func_args_get is passed directly as argument. If you use array_slice (without "p") instead it works the same way without warning.
- When using braces for the inside of the curly braces and matches[1] at the second using, the first trim and substr can be omitted.
	if(preg_match_all('/\{(.+?)\}/', $string, $matches)) {
		$string = str_replace($matches[0], array_map(function($set, $index) { 
			$set = array_map('trim', explode('|', $set));
			return $set[$index > -1 ? $index : array_rand($set)];
		}, $matches[1], array_slice(func_get_args(), 1)), $string);
	}

Open in new window

Good solution, too.
0
 

Author Closing Comment

by:mcainc
ID: 37792089
Wow thanks guys this is perfect!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question