如何使用 PHP 正则表达式将空格和逗号以及空格和逗号的组合替换为任意字符串


How to use PHP regex to replace a space AND comma AND combination of space AND comma with arbitrary string?

如何使用PHP的preg_match来替换:

  1. 空白,以及
  2. 逗号,以及
  3. 空格和逗号的组合

例如,请考虑以下字符串:

apple orange,strawberry, coconut

请注意:

    "
  1. 苹果"和"橙色"之间仅使用空格
  2. "
  3. 橙子"和"草莓"之间只使用逗号
  4. "草莓"和"椰子"之间都使用空格和逗号

如何使用 preg_match 将上面列出的元素的所有出现替换为单词 YES,这将产生以下字符串

appleYESorangeYESstrawberryYEScoconut

提前感谢!

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