-1

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.

I tried using the regex from this another SO question but it does not work.

Community
  • 1
  • 1
Vainglory07
  • 4,713
  • 8
  • 41
  • 76

2 Answers2

2
$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.

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

vks
  • 65,133
  • 10
  • 87
  • 119
1

If your data contains unicode, this should work a bit better:

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

Borrowed with a change from https://stackoverflow.com/a/659030/1935500

Community
  • 1
  • 1
Aziz Saleh
  • 2,652
  • 1
  • 16
  • 26