将 space 添加到更多符号

Adding space to more symbols

例如我有这个字符串

$string="put@returns-between|par/agra\phs";  // @-|/\

如何用一个命令将space添加到每个sympol 这是结果 $result :"put @ returns - | par / agra \ phs 之间 我用过这个解决方案,但我想知道是否有比这个更好的

<?php
$string="put@returns-between|par/agra\phs";  // @-|/\
 $str=preg_replace("/[@]/", " @ ", $string);
  $str=preg_replace("/[-]/", " - ", $str);
  $str=preg_replace("/[|]/", " | ", $str);
  $str=preg_replace("/[\/]/", " / ", $str);

谢谢。

<?php
$string="put@returns-between|par/agra\phs";  // @-|/\
$str=preg_replace("/([@|\-|\||\\|\/])/", "  ", $string);
echo $str;

查看demo,希望对您有所帮助。

使用 character class:

$string = "put@returns-between|par/agra\phs";
$str = preg_replace("~([-@|\\/])~", "  ", $string);
echo $str;

输出:

put @ returns - between | par / agra \ phs