Avatar of error2013
error2013 asked on

PHP Code Modification 2

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

Open in new window



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}

Open in new window


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
PHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
gr8gonzo

Well, you have a problem in that your original code combines all the lines into one array and then you shuffle the values, so you have lost the information that indicates what word was on what line.

If you want each line to contain only words relevant to each other, then I would suggest tracking a separate array $wordLines instead:

//now we need one big array - no, scratch that, one array of other arrays
$wordLines = array();
foreach (explode("\n", $words) as $wordLine) {
	if (!empty($wordLine)) {
                $arrWordLine = explode("|", trim($wordLine, '{}'));
                $wordLines[] = $arrWordLine;
	}
}

$newArticle = $article;
foreach($wordLines as $searchWords)
{
  //replacement words
  $replaceWords = $searchWords;

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

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

var_dump($newArticle);

Open in new window

Ray Paseur

I think you may get a better answer to this question if you tell us where the words come from.  If they are hard-wired into your script, as you are showing us here, then you want a different data structure, as I showed in the other question.  If they are coming from an external source, please show us the external source.   I have a feeling that the abstraction layer of seeing the words hard-wired may be what is preventing you from getting the best answer.
ASKER CERTIFIED SOLUTION
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes