Replace multiple words with Str_Replace

白昼怎懂夜的黑 提交于 2020-01-21 05:15:08

问题


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.


回答1:


$a = array( 'truck', 'vehicle', 'seddan', '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('Lexus','Toyota',$str));



回答2:


You should use strtr

echo strtr($str,array_combine($a,$b)); 

Or Just combine $a and $b into one array

$ab = array('truck' => 'car','vehicle' => 'car','sedan' => 'var','coupe' => 'var','Toyota' => 'Lexus');
echo strtr($str, $ab);

Output

Honda is a car. 
Lexus is a car. 
Nissan is a car. 
Scion is a car.



回答3:


Something like

$a = array( 'truck', 'vehicle', 'seddan', 'coupe' ); 

$str = 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.'; 

echo str_replace($a,'car',$str); 

Should work.

http://codepad.org/1LhtcOSR

Edit:

Something like this should yield expected results: http://pastebin.com/xGzYiCk3

$text = '{test|test2|test3} some other stuff {some1|some2|some3}';

Output:

test3 some other stuff some1
test2 some other stuff some2
test3 some other stuff some3
test3 some other stuff some1



回答4:


I found a very easy solution to replace multiple words in a string :

<?php
 $str="    Honda is a truck. 
Toyota is a vehicle. 
Nissan is a sedan. 
Scion is a coupe.";


$pattern=array();
$pattern[0]="truck";
$pattern[1]="vehicle";
$pattern[2]="sedan";
$pattern[3]="coupe";



$replacement=array();
$replacement[0]="car";
$replacement[1]="car";
$replacement[2]="car";
$replacement[3]="car";



echo str_replace($pattern,$replacement,$str);?> 

output :

  Honda is a car. 
Toyota is a car. 
Nissan is a car. 
Scion is a car

With this script you can replace as many words in a string as you want :

just place the word (that you want to replace) in the pattern array , eg :

     $pattern[0]="/replaceme/"; 

and place the characters (that will be used in place of the replaced characters) in the replacement array, eg :

      $replacement[0]="new_word"; 

Happy coding!




回答5:


For replacing phrases with other phrases (multiple at a time). You can pass arrays as parameters to str_replace()

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = ["fruits", "vegetables", "fiber"];
$yummy   = ["pizza", "beer", "ice cream"];

$newPhrase = str_replace($healthy, $yummy, $phrase);


来源:https://stackoverflow.com/questions/12665743/replace-multiple-words-with-str-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!