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 Link to comment https://forums.phpfreaks.com/topic/80389-solved-regex-to-get-rid-of-symbols-explicit-style/ 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. Link to comment https://forums.phpfreaks.com/topic/80389-solved-regex-to-get-rid-of-symbols-explicit-style/#findComment-408011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.