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

3
  • Is every |*-*| random, or is one random word chosen and then used for all |*-*|? Commented Feb 27, 2012 at 8:30
  • each one a random word... so different words through the string Commented Feb 27, 2012 at 8:31
  • Use the random() function to get a random index, and then replace |*-*| by your Array[your_random_index] Commented Feb 27, 2012 at 8:31

6 Answers 6

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Will replace all items with the first randomly chosen item, wont it?
nope. I've put a 1 as 4th parameter. So it will only replace 1 occurence
4th parameter gives me a warning!
@Topener the last argument needs to be a reference
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

Comments

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;

Comments

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);
?>

Comments

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.

Comments

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

1 Comment

The OP wants random words to be filled into a pattern. In this case, it will replace the character array |*-*|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.