Solved

PHP string randomization with Regular Expressions?

Posted on 2012-03-30
6
341 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitting a sting into elements 6 77
Do not understand error message 3 46
how to use jquery variable in mysql query 20 77
Developers / Staff Setup 10 48
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 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…

710 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