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

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

0
Ray PaseurCommented:
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.
0
Chris StanyonWebDevCommented:
I would do the replacement inside the line loop, and not bother creating a large 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}
{word66|word23|word32|word47|word56}
{word13|word22|word31|word46|word55}
';

$searchWords = array();
foreach (explode("\n", $words) as $wordLine) {
	if (!empty($wordLine)) {
		$searchWords = explode("|", trim($wordLine, '{}'));
		$replaceWords = $searchWords;
		shuffle($replaceWords);
		
		$article = str_replace($searchWords, $replaceWords, $article);
	}
}

var_dump($article);

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.