Str_replace多个项目 [英] Str_replace for multiple items
本文介绍了Str_replace多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我记得以前曾经做过,但是找不到代码.我使用str_replace替换一个这样的字符:str_replace(':', ' ', $string);
,但我想替换以下所有字符\/:*?"<>|
,而不为每个字符替换str_replace.
I remember doing this before, but can't find the code. I use str_replace to replace one character like this: str_replace(':', ' ', $string);
but I want to replace all the following characters \/:*?"<>|
, without doing a str_replace for each.
推荐答案
str_replace()
can take an array, so you could do:
$new_str = str_replace(str_split('\\/:*?"<>|'), ' ', $string);
或者,您可以使用 preg_replace()
:
Alternatively you could use preg_replace()
:
$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
这篇关于Str_replace多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文