基本上我有一个代码,当你输入数组中列出的单词时,它会随机替换另一个数组中的一个单词。在这种情况下,我想让$bye中的单词被$hello中的单词替换,$sanimal的单词被$Color随机替换。
这一切都很好,尽管我想知道的是,我该如何制作它,这样在回声中它就不会显示两次,而只是组合它
例如,如果我输入"再见,我的猫",它会给我的"嗨,我的红"即"哟,我的猫儿再见,我红"。
这是我当前的代码:
$hello=array('hello', 'hi', 'yo'); //$replacements
shuffle($hello);
$animal = array('/mouse/', '/cat/', '/dog/'); //pattern
$colour = array('yellow', 'blue', 'red'); //$replacements
shuffle($dog);
echo preg_replace($bye, $hello, $words);
echo preg_replace($mouse, $dog, $words);
?>
echo preg_replace($mouse, $dog, preg_replace($bye, $hello, $words));
你可以把它们组合起来。。
或者将第一次替换的结果分配回$words
,然后进行第二次
$words = preg_replace($bye, $hello, $words);
echo preg_replace($mouse, $dog, $words);
可能需要添加
echo '<br>';
在第一次怀孕后?即使不是preg_replace,这里也适合str_replace
我稍微修改了您的代码。。。。这是一个概念问题:
<?php
$words= "bye my cat";
echo $words."</br>";
$hello=array('hello', 'hi', 'yo'); //$replacements
shuffle($hello);
$pattern = array('/bye/', '/cat/', '/dog/'); //pattern
$replacements = array('hello', 'red', 'blue'); //$replacements
echo preg_replace($pattern, $replacements, $words);
?>
我希望你能清楚XD
PS:输出很好。
Saludos。