【问题标题】:How can I strip commas and periods from a string?如何从字符串中删除逗号和句点?
【发布时间】:2011-05-27 04:01:45
【问题描述】:

我的字符串包含这样的内容:

"Favourite bands: coldplay, guns & roses, etc.,"

如何使用preg_replace 删除逗号和句点?

【问题讨论】:

    标签: php regex preg-replace


    【解决方案1】:

    你可以使用

    preg_replace('/[.,]/', '', $string);
    

    但是使用正则表达式进行简单的字符替换是多余的。

    你最好使用strtr

    strtr($string, array('.' => '', ',' => ''));
    

    str_replace:

    str_replace(array('.', ','), '' , $string);
    

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2019-01-10
      相关资源
      最近更新 更多