【发布时间】:2012-03-12 17:27:01
【问题描述】:
我希望能够用- 替换空格,但也希望删除逗号和问号。我怎样才能在一个函数中做到这一点?
到目前为止,我已经用它替换了空格:
str_replace(" ","-",$title)
【问题讨论】:
标签: php string str-replace
我希望能够用- 替换空格,但也希望删除逗号和问号。我怎样才能在一个函数中做到这一点?
到目前为止,我已经用它替换了空格:
str_replace(" ","-",$title)
【问题讨论】:
标签: php string str-replace
您可以将数组作为参数传递给str_replace()。检查manual。
// 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);
【讨论】:
str_replace 依次替换每个项目,但您也可以使用strtr 同时替换项目。