如何使用PHP正则表达式替换空格AND逗号和空格AND逗号与任意字符串的组合?

时间:2013-01-31 05:25:25

标签: php regex preg-match

如何使用PHP的preg_match替换:

  1. 白色空间,
  2. 逗号,
  3. 空格和逗号的组合
  4. 例如,请考虑以下字符串:

    apple orange,strawberry, coconut
    

    请注意:

    1. “apple”和“orange”之间仅使用空格
    2. “orange”和“strawberry”
    3. 之间仅使用逗号
    4. 白色空格和逗号都用在“草莓”和“椰子”之间
    5. 如何使用preg_match将上面列出的元素的所有出现替换为单词YES,从而产生以下字符串

      appleYESorangeYESstrawberryYEScoconut
      

      提前致谢!

2 个答案:

答案 0 :(得分:5)

preg_replace('/[\s,]+/', 'YES', $str);

答案 1 :(得分:1)

preg_replace('/\s+|\s*,\s*/', 'YES', $str);
相关问题