dsaba Posted December 6, 2007 Share Posted December 6, 2007 $str = 'wh*?t do you want to do today? H*re is another *sterisk.'; $symbols = preg_quote('/\*!@#$%^&[](){}-_+=<>.,?~`:;."|\'','~'); //$symbols = '\*\?'; $pat = '~['.$symbols.']*~'; $str = preg_replace($pat, '', $str); echo $str; I get this output: Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 29 How can I fix it/get rid of this error? I'm simply trying to use preg_replace() to explicitly get rid all symbols in a string using regex. I realize i can put them into an array with preg_replace(), and what I mean by explicit is that I look for symbols, and if I find them I replace them with nothing ( '' ), so I don't want to say [^a-zA-Z] Thank you Quote Link to comment Share on other sites More sharing options...
effigy Posted December 6, 2007 Share Posted December 6, 2007 When hyphens are used inside of a character class they become a ranging metacharacter, e.g., [A-Z]. You have }-_ within your character class which is an invalid range. The best practice for this is to always put the hyphen first in a character class, then it can never be mistaken for a range. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.