PHP - Preg替换模式来替换/或&如果有必要,有空格或没有空格


PHP - Preg replace pattern to replace / or & with space or without space if necessary

用有空格和不带空格的and代替/&preg_replace模式是什么?该模式应该能够替换以下所有示例:

This is J/J. to This is J and J.

J/J, Y& S, and U / A are names. to J and J, Y and S and U and A are names.

What does J /J or J & E mean?What does J and J or J and E mean?

试试这个:

<?php
   $text = "J/J, Y& S, and U / A are names.";
   $result = preg_replace('~[/&]~', ' and ', $text, -1);
   echo $result;