Link to home
Start Free TrialLog in
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
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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

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
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial