monkeytooth Posted November 12, 2009 Share Posted November 12, 2009 I'm trying to figure out the best way to determine and or find out if a string only has a-Z 0-9 - _ . in it and nothing else, but I dunno what would be the best route to go with that. Anyone have ideas? Link to comment https://forums.phpfreaks.com/topic/181208-strings-that-only-have-a-z-1-9-_/ Share on other sites More sharing options...
Garethp Posted November 12, 2009 Share Posted November 12, 2009 if(preg_match('~[^a-zA-Z0-9\-_.]~', $String)) { //The string contains other characters } Link to comment https://forums.phpfreaks.com/topic/181208-strings-that-only-have-a-z-1-9-_/#findComment-955985 Share on other sites More sharing options...
monkeytooth Posted November 12, 2009 Author Share Posted November 12, 2009 hmm.. ok, will give that a shot right now. thank ya Link to comment https://forums.phpfreaks.com/topic/181208-strings-that-only-have-a-z-1-9-_/#findComment-955987 Share on other sites More sharing options...
oni-kun Posted November 12, 2009 Share Posted November 12, 2009 Yes, You'd use a Regular Expression ( REGEX ) pattern to find if a string contains certain characters. You can perform one like so: function CheckString($str) { if (preg_match('~[^a-zA-Z0-9\-_.]~', $str)) { echo "String is VALID;" //1 } else { echo "STRING CONTAINS INVALID CHARACTERS" //0 } } $txt = "A-Z-0_2_3_4_5"; $txt2 = "0-F-*-@-K"; CheckString($txt); CheckString($txt2); Link to comment https://forums.phpfreaks.com/topic/181208-strings-that-only-have-a-z-1-9-_/#findComment-955988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.