如何在PHP中替换文本字符串中的多个项目?

2022-08-30 09:42:00

我希望能够用空格替换,但也想删除逗号和问号。如何在一个函数中执行此操作?-

到目前为止,我用它替换了空格:

str_replace(" ","-",$title)

答案 1

可以将数组作为参数传递给 。查看手册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);

答案 2

推荐