简体   繁体   English

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

[英]Get all alphabets in a string of words using regex (including spaces)

How would I extract all alpha characters (including space) like for example: 我将如何提取所有字母字符(包括空格),例如:

@john camel07 st.doe!

where I only want to get john camel stdoe . 我只想得到john camel stdoe

I tried using the regex from this another SO question but it does not work. 我尝试从另一个SO问题中使用正则表达式,但是它不起作用。

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

$result = preg_replace($re, $subst, $str);

You can simply replace by empty string all non alpha and space characters.See demo. 您可以简单地用empty string替换所有非字母和空格字符。请参见演示。

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

If your data contains unicode, this should work a bit better: 如果您的数据包含unicode,则效果会更好一些:

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

Borrowed with a change from https://stackoverflow.com/a/659030/1935500 借用https://stackoverflow.com/a/659030/1935500的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM