I would like to replace all special characters in a string except space and -.
For example,
Hello-my näme *is (Jämes93!
to
Hello-my name is James93
or even
Hello-my nme is Jmes93
I have the following but it won’t work. Pls can someone help?
Thanks
preg_replace('#[^\w-]#',"",$string)
You don’t want to use regex for these. You are much better off using
iconv()with transliteration:
This assumes that original string is encoded in
UTF-8and you want to convert it toASCIIwhich comes close to your question.You can convert to/from other charsets the same way just as long as you know the original charset.
By the way, you can’t do that with
preg_replace, it does not support transliteration.