1

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

Salman A
  • 262,204
  • 82
  • 430
  • 521
Adam
  • 19,932
  • 36
  • 124
  • 207

6 Answers6

2

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);
}
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
2

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

Community
  • 1
  • 1
silly
  • 7,789
  • 2
  • 24
  • 37
0

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.
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

Try this

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

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;
Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
-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.

  • The OP wants random words to be filled into a pattern. In this case, it will replace the character array |*-*| – sukalogika Nov 23 '21 at 01:46