替换字符串中除字母和空格外的所有内容

时间:2015-12-27 00:05:25

标签: regex preg-replace

我正在尝试替换除字母和空格之外的字符串中的所有内容。我该怎么做?

$str = "one two three !@#$%^&*()_+|";
$str = preg_replace('/\PL/u', '', $str);
echo $str;

结果:

onetwothree

想要结果:

one two three

1 个答案:

答案 0 :(得分:4)

$str = preg_replace('/[^\p{L} ]+/u', '', $str);

See it in action

我们的想法是替换非(+)字母(^)或空格的倍数(\p{L})。

相关问题