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
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

 

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.