PHP Code Modification

Hi all,

I have the following code which I need modifying.

$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


The code works great but the search replace words are not working properly in multiple lines:

For example:

$words = '
{word1|word2|word3|word4|word5}
{word66|word23|word32|word47|word56}
{word13|word22|word31|word46|word55}
';

Open in new window


Can anyone help 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.

Marco GasiFreelancerCommented:
Try this:

$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));
$words = "/" . $words . "/"; //this line is added to work with preg_replace function

//replacement words
$newWords = $words;

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

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

echo $newArticle;
                                  

Open in new window

0
error2013Author Commented:
I'm getting this error:

Warning: shuffle() expects parameter 1 to be array, string given in /home4/public_html/index.php on line
0
Chris StanyonWebDevCommented:
If that's how your words are coming in, then you will need the code to create one large array of words. The code below creates an array of each line, and loops through that array, creating an array of words from each line, and then merges them into one big array. Make sense??

$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

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
Ray PaseurCommented:
Please correct me if I'm wrong, but this looks like an attempt to randomize some text so that it carries the same meaning but looks different to search engines. The degree to which you control the programming and data will be useful in getting you the best solution.  Are you stuck with the format of the search and replace words' strings?  If not, I can suggest a little bit different design that would make things easier.  If you write the words in the form of arrays as shown here, the process of editing and the programming to use the arrays is somewhat easier and less prone to typographical errors.
http://www.laprbass.com/RAY_temp_error2013.php

<?php // RAY_temp_error2013.php
error_reporting(E_ALL);
echo '<pre>';

$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.';
echo PHP_EOL . $article;


// WORDS TO BE REPLACED IN THE FORM OF AN ARRAY
$words = array
( 'word1'
, 'word2'
, 'word3'
, 'word4'
, 'word5'
, 'word66'
, 'word23'
, 'word32'
, 'word47'
, 'word56'
, 'word13'
, 'word22'
, 'word31'
, 'word46'
, 'word55'
)
;

//replacement words
$newWords = $words;

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

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

Open in new window

HTH, ~Ray
0
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.