PHP Search and Replace String Randomly

Hi all,

I have the 2 variables/strings below:

$article = ('Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.');

$words = '{word1|word2|word3|word4|word5}';

What I need to do is to search $article for any words in $word and if found randomly replace the word found in $article with a random picked word from $word.

How can I do that please?
LVL 1
error2013Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Have a look at this and see how you get on:

<?php 
$article = 'Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.';

//words to be repalced
$words = array( 'word1', 'word2', 'word3', 'word4', 'word5' );

//replacment words
$newWords = array('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8');

//randomise the new words
shuffle($newWords);

//perform the replacement
$newArticle = str_replace($words, $newWords, $article);

echo $newArticle;
?>

Open in new window

0
error2013Author Commented:
Hi... just one problem.

I get the words in this format:

$words = '{word1|word2|word3|word4|word5}';

So it needs to be processed in that format.
0
Chris StanyonWebDevCommented:
No problem. Just strip the {}  and then explode into an array:

$article = 'Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.';

//words to be replaced
$words = '{word1|word2|word3|word4|word5}';
$words = explode("|", substr($words,1,-1));

//replacment words
$newWords = array('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8');

//randomise the new words
shuffle($newWords);

//perform the replacement
$newArticle = str_replace($words, $newWords, $article);

echo $newArticle;

Open in new window

0
error2013Author Commented:
Almost...

Just 1 thing ...

The Replacement string should be

$words = '{word1|word2|word3|word4|word5}';

One of the above picked at random.

Apart from that all great.
0
Chris StanyonWebDevCommented:
$article = 'Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.';

//words to be replaced
$words = '{word1|word2|word3|word4|word5}';
$words = explode("|", substr($words,1,-1));

//replacement words
$newWords = $words;

//randomise the new words
shuffle($newWords);

//perform the replacement
$newArticle = str_replace($words, $newWords, $article);

echo $newArticle;

Open in new window

:)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.