我有这样的代码
$str = "PHP is programing languange. I can speak English";
$replaceWith = [
[PHP,JAVA,Python],[English,Japanese,Italian]
];
如果要在数组中有某个单词而不要再次显示该单词,我希望这样替换文本。
[JAVA,Python]正在编程languange。我会说[日语,意大利语]
答案 0 :(得分:0)
为什么不将字符串拆分为数组并在符合条件时替换它们?
类似这样的东西:
$str = "PHP is programing languange. I can speak English";
$arr = preg_split('/\s+/', $str);
$newStr = "";
foreach ($arr as $key=>$value)
{
if ($value == "PHP")
{
$value = "Java, Python";
}
else if ($value == "Java")
{
$value = "PHP, Python";
}
else if ($value == "Python")
{
$value = "Java, Python";
}
$newStr.= " ".$value;
}
// Output
// $newStr = "Java, Python is programing languange. I can speak English";
虽然不漂亮,但可以完成工作。