-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
  • 5,073
  • 10
  • 43
  • 77

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
  • 67,027
  • 10
  • 91
  • 124
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,687
  • 1
  • 17
  • 27