Torrodon Posted June 7, 2009 Share Posted June 7, 2009 I need to match all symbols except letters, numbers, "_", " ", "-", "&"? I tried the following but it is not working: preg_match_all ( '/[^a-z0-9_ -&]/i' , $string , $matches ); Quote Link to comment https://forums.phpfreaks.com/topic/161267-help-matching-all-symbols-except-defined/ Share on other sites More sharing options...
nrg_alpha Posted June 7, 2009 Share Posted June 7, 2009 When using a dash in a character class, put it as the first or last position (in this case, due to negation, the second or last position): preg_match_all ( '/[^a-z0-9_ &-]/i' , $string , $matches ); Otherwise, regex will treat the mal-positioned dash as a range.. in your initial case, a range from space to & (which is not what you want). Quote Link to comment https://forums.phpfreaks.com/topic/161267-help-matching-all-symbols-except-defined/#findComment-850965 Share on other sites More sharing options...
Torrodon Posted June 9, 2009 Author Share Posted June 9, 2009 grrr i forgot '-' is special symbol inside [] and i have to escape it preg_match_all ( '/[^a-z0-9_ \-&]/i' , $string , $matches ); this should work i'm going to test it now thanks Quote Link to comment https://forums.phpfreaks.com/topic/161267-help-matching-all-symbols-except-defined/#findComment-852046 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.