如何使用随机选择的数组元素替换子字符串?

时间:2017-06-02 01:27:42

标签: php random replace preg-match substring

只是评论: 在上一个问题中,我需要将值从第0列更改为1或1更改为0,具体取决于$myVar变量中解析的任何值,用户在stackoverflow上附加的解决方案是理想的:< / p>

$myWords=array(
    array('funny','sad'),
    array('fast','slow'),
    array('beautiful','ugly'),
    array('left','right'),
    array('5','five'),
    array('strong','weak')
);

// prepare values from $myWords for use with strtr()
$replacements=array_combine(array_column($myWords,0),array_column($myWords,1))+
              array_combine(array_column($myWords,1),array_column($myWords,0));
echo strtr($myVar,$replacements);

输入/输出:

   $myVar='I was beautiful and strong when I was 5 now I\'m ugly and weak';
//outputs: I was ugly and weak when I was five now I'm beautiful and strong

此问题:

但是在某些情况下,您是否看到有多个选项的数组进行切换?如何让系统对多个选项中的任何单词进行交换,但不存在做“回声”的风险。使用最初在$myVar中显示的关键/单词/关键字/单词调用证券交易所行动?

$myWords=array(
    array('funny','sad','very sad','hyper funny'),
    array('fast','slow','faster','very slow'),
    array('beautiful','Studious man','great beauty','very hardworking'),
);

$myVar = 'That man is really fast and very hardworking';

如何使系统选择其他选项,但它从交换或rand或mt_rand等中排除...,负责调用操作的键:快速,勤奋,以免冒{{{ 1}}不能改变。

可能的预期输出:

$myVar

$myVar = 'That man is really faster and Studious man'; 不得替换为fastfast不得替换为very hardworking

1 个答案:

答案 0 :(得分:1)

我认为这就是你要做的......

检查字符串是否从每个子数组中随机选择。 如果匹配,则要将其替换为同一子阵列中的任何其他单词。 如果没有匹配,只需转到下一个子阵列。

$myWords=array(
    array('funny','sad','very sad','hyper funny'),
    array('fast','slow','faster','very slow'),
    array('beautiful','Studious man','great beauty','very hardworking'),
);

foreach($myWords as $words){
    // randomize the subarray
    shuffle($words);
    // pipe-together the words and return just one match

if(preg_match('/ \ b \ K'.implode('|',$ words)。'\ b /',$ myVar,$ out)){错误模式=不正确使用\b

    if(preg_match('/\K\b(?:'.implode('|',$words).')\b/',$myVar,$out)){
        // generate "replace_pair" from matched word and a random remaining subarray word
        // replace and preserve the new sentence
        $myVar=strtr($myVar,[$out[0]=>current(array_diff($words,$out))]);
    }
}
echo $myVar;

如果:

$myVar='That man is really fast and very hardworking';

然后输出可以是以下任何一种以上:

That man is really faster and great beauty
That man is really slow and Studious man
etc...

实际上,无论发生什么随机替换,输出都不会与输入相同。

这是demo link

这是preg_match_all()版本:

$myWords=array(
    array('funny','sad','very sad','hyper funny'),
    array('fast','slow','faster','very slow'),
    array('beautiful','Studious man','great beauty','very hardworking'),
);

$myVar='The slow epic fail was both sad and funny';

foreach($myWords as $words){
    $replacepairs=[];  // clear array

if(preg_match_all('/ \ b \ K'.implode('|',$ words)。'\ b /',$ myVar,$ out)){错误模式=不正确使用\b

    if(preg_match_all('/\K\b(?:'.implode('|',$words).')\b/',$myVar,$out)){  // match all occurences
        foreach($out[0] as $w){
            $remaining=array_diff($words,[$w]);  // find the remaining valid replacment words
            shuffle($remaining);                 // randomize the remaining replacement words
            $replacepairs[$w]=current($remaining);  // pluck first value from remaining words
        }
        $myVar=strtr($myVar,$replacepairs);      // use replacepairs on sentence
    }
}
echo $myVar;

可能的输出:

The faster epic fail was both hyper funny and hyper funny
The very slow epic fail was both very sad and hyper funny
The fast epic fail was both funny and very sad
etc...

这是demo link