Link to home
Start Free TrialLog in
Avatar of error2013
error2013

asked on

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?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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

Avatar of error2013
error2013

ASKER

I'm getting this error:

Warning: shuffle() expects parameter 1 to be array, string given in /home4/public_html/index.php on line
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
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