如何在php中删除标点符号

oca*_*nal 13 php string strip

除了这些字符外,如何删除标点符号 . = $ ' - %

Wai*_*rim 31

这是一个巧妙的方法:

preg_replace("#[[:punct:]]#", "", $target);
Run Code Online (Sandbox Code Playgroud)


mar*_*rio 20

由于您需要匹配一些Unicode字符(),因此使用正则表达式是明智的.该模式\p{P}匹配任何已知的标点符号,并且断言将您想要的特殊字符从消失中排除:

 $text = preg_replace("/(?![.=$'€%-])\p{P}/u", "", $text);
Run Code Online (Sandbox Code Playgroud)


Shi*_*ryu 8

<?
$whatToStrip = array("?","!",",",";"); // Add what you want to strip in this array
$test = "Hi! Am I here?";
echo $test."\n\n";
echo str_replace($whatToStrip, "", $test);
Run Code Online (Sandbox Code Playgroud)

在这里演示

或者,当然,更短:

$test = str_replace(array("?","!",",",";"), "", $test);
Run Code Online (Sandbox Code Playgroud)

来自str_replace手册的第一个例子