Replace multiple words with Str_Replace

后端 未结 5 2233
醉梦人生
醉梦人生 2021-01-05 18:50

I want to replace multiple synonyms with one specific word.



        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 19:15

    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);
    

提交回复
热议问题