Solved

PHP string randomization with Regular Expressions?

Posted on 2012-03-30
6
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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 look for a specific file type in a local or remote server directory using PHP.

615 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