Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP string randomization with Regular Expressions?

Posted on 2012-03-30
6
Medium Priority
?
347 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
animecyc earned 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

877 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