使用正则表达式(包括空格)获取字符串中的所有字母



我如何提取所有字母字符(包括空格(,例如:

@john camel07 st.doe!

我只想得到CCD_ 2。

我尝试使用另一个SO问题中的正则表达式,但它不起作用。

$re = "/[^a-zA-Z ]+/"; 
$str = "@john camel07 st.doe!"; 
$subst = ""; 
$result = preg_replace($re, $subst, $str);

您可以简单地用empty string替换所有非alpha和空格字符。请参阅演示。

https://www.regex101.com/r/rL8wP1/7

如果您的数据包含unicode,这应该会更好地工作:

echo preg_replace("/[^[:alpha:][:space:]]/ui", '', '@john camel07 st.doe!');

借自https://stackoverflow.com/a/659030/1935500

最新更新