Reputation: 1733
Is there a general used regex that removes ALL hyphens, special characters etc., so I will only get letters.
For example a regex containing: ,./<>?;':"|[]{}-=_+1234567890!@#$%^&*()|\ ~` and all of the hyphens and special characters.
(don't know if this is called a regex, but I hope you get the idea)
Upvotes: 5
Views: 11148
Reputation: 4508
This should work :
$string = preg_replace("/[^a-zA-Z]+/", "", $string);
if you want to keep numbers, use this one :
$string = preg_replace("/[^a-zA-Z0-9]+/", "", $string);
Upvotes: 10
Reputation: 626871
If you need to get only letters, remove all that are not letters:
preg_replace('~\P{L}+~u', '', $input)
The \P{L}
is a Unicode property that matches all characters other than Unicode letters. See the regex demo.
If you need to also handle diacritics (i.e. if you need to keep them), use
preg_replace('~[^\p{M}\p{L}]+~u', '', $input)
where \p{M}
matches any diacritic symbol, and [^\p{M}\p{L}]+
matches 1 or more characters other than letters and diacritics.
See another demo.
Upvotes: 3