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)
<?
$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)