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 ); 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). 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 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
Archived
This topic is now archived and is closed to further replies.