Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple words with 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.

like image 373
Jim B Bob Avatar asked Sep 30 '12 22:09

Jim B Bob


1 Answers

$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));
like image 153
Eugen Rieck Avatar answered Sep 20 '22 20:09

Eugen Rieck