troubleshooting Question

PHP Code Modification 2

Avatar of error2013
error2013 asked on
PHP
3 Comments1 Solution310 ViewsLast Modified:
Hi all,

I need another modification to the same code I asked for on my last question.

Here is the working code:

$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}
{word66|word23|word32|word47|word56}
{word13|word22|word31|word46|word55}
';

//now we need one big array
$searchWords = array();
foreach (explode("\n", $words) as $wordLine) {
	if (!empty($wordLine)) {
		$searchWords = array_merge($searchWords, explode("|", trim($wordLine, '{}')));
	}
}

//replacement words
$replaceWords = $searchWords;

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

$newArticle = str_replace($searchWords, $replaceWords, $article);

var_dump($newArticle);


Currently the words that are to be replaced are chosen from the BIG array.

What I need is that it only picks a random word to replace from it's line.

For example, if we have this:

{word1|word2|word3|word4|word5}
{word66|word23|word32|word47|word56}
{word13|word22|word31|word46|word55}

If is finds a match on line 2 say word66 then it should find a random word from that line (line2) to be replace and not from the whole of the lines.

If word1 is a match with the $article then is will replace randomly one of the words on the first line.

And so on.

How can I do this please?

Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros