Adam
Adam

Reputation: 20882

PHP randomly replace words

I have an array called $featuresSEO that has a number of words in it like:

Array (
    [0] => Japan 
    [1] => Japanese 
    [2] => Tokyo 
    [3] => Yokohama 
    [4] => Osaka 
    [5] => Asian 
    [6] => Nagoya 
) 

I then have a string like follows:

 Searching the |*-*| Dating membership database is the key to locating |*-*| people
 you would be interested in. You can search for |*-*| Singles including |*-*| Women
 and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.

I've been trying to replace the instances of |*-*| with random words from the Array. I've tried str_replace() but couldn't get the random aspect working.

Can someone push me in the right direction?

thx

Upvotes: 1

Views: 1381

Answers (6)

mickmackusa
mickmackusa

Reputation: 47894

PHP already has a native function which allows you to replace placeholders in a template string with an array of strings -- vprintf() variable print from format if I am not mistaken.

If you had originally set up your template with recognised placeholders, you wouldn't need to call str_replace().

Code: (Demo)

$template = 'Searching the |*-*| Dating membership database is the key to locating |*-*| people
 you would be interested in. You can search for |*-*| Singles including |*-*| Women
 and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.';

$array = ['Japan', 'Japanese', 'Tokyo', 'Yokohama', 'Osaka', 'Asian', 'Nagoya'];

shuffle($array);
vprintf(str_replace('|*-*|', '%s', $template), $array);

Potential output:

Searching the Asian Dating membership database is the key to locating Japan people
 you would be interested in. You can search for Osaka Singles including Nagoya Women
 and Yokohama Men in any location worldwide. Join now to search for Tokyo Singles.

Upvotes: 0

Anonizmus User
Anonizmus User

Reputation: 1

I hope this is still relevant)

$wordarray = [
   'some',
   'random',
   'words',
   'list'
];
shuffle($wordarray);
$string = implode(' ', $wordarray);

You can change implode to sprintf, that was my realization.

Upvotes: -1

silly
silly

Reputation: 7887

try this

$string = ' Searching the |*-*| Dating membership database is the key to locating |*-*| people
 you would be interested in. You can search for |*-*| Singles including |*-*| Women
 and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.';

$words = array('Japanese', 'Tokyo', 'Asian');

$placeholder = '|*-*|';
$pos = null;

while(null === $pos || false !== $pos) {
    $pos = strpos($string, $placeholder);
    $string = substr_replace($string, $words[rand(0, count($words)-1)], $pos, strlen($placeholder));

}

echo $string;

first word become unexpected

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272106

The code for replace-only-first-match is borrowed from here. The following code uses each word from the list only once:

$wordlist = array(
    'Japan',
    'Japanese',
    'Tokyo',
    'Yokohama',
    'Osaka',
    'Asian',
    'Nagoya'
);
$string = "
Searching the |*-*| Dating membership database is the key to locating |*-*| people
you would be interested in. You can search for |*-*| Singles including |*-*| Women
and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.
";
$replace = '|*-*|';
while(true){
    $index = strpos($string, $replace);
    if($index === false){
        // ran out of place holder strings
        break;
    }
    if(count($wordlist) == 0){
        // ran out of words
        break;
    }
    $word = array_splice($wordlist, rand(0, count($wordlist) - 1), 1);
    $string = substr_replace($string, $word[0], $index, strlen($replace));
}
echo $string;

Upvotes: 0

Mattias
Mattias

Reputation: 9481

Try this

<?php
    $array = array("Japan","Japanese","Tokyo","Yokohama","Osaka","Asian","Nagoya");
    $a = array_rand($array);
    $string= "abc|*-*|";
    echo str_replace("|*-*|", $array[$a], $string);
?>

Upvotes: 0

Rene Pot
Rene Pot

Reputation: 24815

Replace them one by one. This one will replace each occurence with a random word. You might see the same word from the $wordarray multiple times as it picks 1 at random each time.

for ($i = 0; $i < substr_count($string, '|*-*|'); $i++){
     $string = preg_replace('/\|\*-\*\|/',$wordarray[rand(0,count($wordarray)-1)],$string, 1);
}

Want to use each word only once? Shuffle the array, and loop through it:

shuffle($wordarray);
foreach ($wordarray as $word){
    $string = preg_replace('/\|\*-\*\|/',$word,$string,1);
}

Upvotes: 2

Related Questions