Solved

PHP string randomization with Regular Expressions?

Posted on 2012-03-30
6
331 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now