regex - Remove all hyphens, special characters etc. from string in PHP
Is there a general used regex that removes ALL hyphens, special characters etc., so I will only get letters.
For example a regex containing: ,./<>?;':"|[]{}[email protected]#$%^&*()|\ ~` and all of the hyphens and special characters.
(don't know if this is called a regex, but I hope you get the idea)
Answer
Solution:
This should work :
if you want to keep numbers, use this one :
Answer
Solution:
If you need to get only letters, remove all that are not letters:
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
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.