PHP随机替换单词


PHP randomly replace words

我有一个名为$featuresSEO的数组,里面有很多单词,比如:

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

然后我有一个字符串如下:

 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.

我一直在尝试用数组中的随机字替换|*-*|的实例。我尝试过str_replace(),但无法使随机方面工作。

有人能把我推向正确的方向吗?

thx

逐个替换它们。这将用一个随机单词替换每个出现的单词。您可能会多次看到$wordarray中的同一个单词,因为它每次都随机选择1。

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

想每个单词只使用一次吗?打乱阵列,并在其中循环:

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

尝试这个

$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;

第一个单词变成意外

仅替换第一个匹配的代码是从这里借来的。以下代码只使用列表中的每个单词一次:

$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;

试试这个

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

PHP已经有了一个本机函数,它允许您用字符串数组替换模板字符串中的占位符——如果我没有弄错的话,vprintf()变量从格式打印。

如果您最初使用已识别的占位符设置模板,则不需要调用str_replace()

代码:(演示)

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

潜在输出:

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.

我希望这仍然相关)

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

你可以把内爆改为sprintf,这是我的意识。