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? Quote Link to comment 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 } Quote Link to comment 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 Quote Link to comment 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); 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.