用Str_Replace替换多个单词 [英] Replace multiple words with Str_Replace
本文介绍了用Str_Replace替换多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用一个特定的单词替换多个同义词.
I want to replace multiple synonyms with one specific word.
<?p
$a = array(
'truck',
'vehicle',
'seddan',
'coupe',
'Toyota',
);
$b = array(
'car',
'car',
'car',
'car',
'Lexus',
);
$str = '
Honda is a truck.
Toyota is a vehicle.
Nissan is a sedan.
Scion is a coupe.
';
echo str_replace($a,$b,$str);
?>
结果:本田是一辆汽车.雷克萨斯是一辆汽车.日产是汽车.子孙是一辆车.
RESULT: Honda is a car. Lexus is a car. Nissan is a car. Scion is a car.
有人可以告诉我一种用汽车"一词代替车辆,卡车,双门轿跑车,轿车"的干净方法,而不是我分别替换所有这四个方法.谢谢.
Can someone show me a clean way of replacing "vehicle, truck, coupe, sedan" with the word "car" instead of me replacing all 4 of them individually. Thank you.
推荐答案
$a = array( 'truck', 'vehicle', 'sedan', 'coupe' );
$str = 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
echo str_replace($a,'car',str_replace('Toyota','Lexus',$str));
这篇关于用Str_Replace替换多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文