Jump to content

[SOLVED] Help with regex


raimis100

Recommended Posts

Hey!

 

I need to get some things done. I guess I will need to use regex.

 

I have a string

(great,cool) site, I (like,enjoyed) it

 

What I should do is make it grab one of the words in brackets

It can randomly be

Great site, I like it

Cool site, I like it

etc.

 

Any suggestion how could I do this ?

Link to comment
https://forums.phpfreaks.com/topic/141090-solved-help-with-regex/
Share on other sites

Well ... This was not what I was acutally looking for.

But I cannot make it work that way.

 

User don't enter arrays and place where it should be randomly picked

User just enters text (great,cool) site, I (like,enjoyed) it

 

Okey, lets try to make it up by pieces.

 

First I need to scrape every word in brackers and then randomly choose one word from it.

 

After that brackets and content it in should be replaced with that one randomly choosen word

 

<pre>
<?php
$data = <<<DATA
(great,cool) site, I (like,enjoyed) it
i love (apples,oranges,bananas,grapes,tangerines,cake,pie)
DATA;

echo preg_replace_callback(
	'/\((.*?)\)/', 
	create_function(
		'$words',
		'$words = split(",", $words[1]);
		 return array_rand(array_flip($words));'
	),
	$data
);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.