etnastyles Posted January 15, 2008 Share Posted January 15, 2008 help please, all this regex and preg_match is very confusing - i have been searching the net for examples but most of the ones i find relate to url searching or the [a-z] classic . what im looking for or help with is finding characters that arent meant to be in a form that can be submitted to a mysql db i.e. \, *, +, =, -, !, ? etc.. a backslash \ preg_match would be gd. could anyone give me a pinter or point me in the correct direction. thks Quote Link to comment Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 characters that arent meant to be in a form that can be submitted to a mysql db I don't follow you here. Anything can be submitted to MySQL. The real question might be: What do you want to allow in your database? Quote Link to comment Share on other sites More sharing options...
etnastyles Posted January 15, 2008 Author Share Posted January 15, 2008 im looking for a reg or preg_match that denys anything thats not a-z 0-9 i dont want slashes \ / or symbols like * or @ ~ " ^ ( ) mysql is great Quote Link to comment Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 <pre> <?php echo preg_match('/[^a-zA-Z0-9]/', 'Invalid string!') ? 'Invalid' : 'Valid' ; ?> </pre> Quote Link to comment Share on other sites More sharing options...
etnastyles Posted January 15, 2008 Author Share Posted January 15, 2008 whats this bit for ? == ) ? 'Invalid' : 'Valid' ; is this some logic happening here on the fly - will this return true false so you dont need to echo out a crappy msg? echo yay else echo boo Quote Link to comment Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 Right. It's the ternary operator. Quote Link to comment Share on other sites More sharing options...
etnastyles Posted January 15, 2008 Author Share Posted January 15, 2008 thks for your help buddy - must check out ternary aspitnf Quote Link to comment Share on other sites More sharing options...
etnastyles Posted January 15, 2008 Author Share Posted January 15, 2008 hows about this then define("REG_VALID_FORM", '/[^a-zA-Z0-9]/', '\/*.!@~#+,"|[]{}:;'); which one of these fun symbols represent white space??? Quote Link to comment Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 \s is the shorthand for whitespace. Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 <pre> <?php echo preg_match('/[^a-zA-Z0-9]/', 'Invalid string!') ? 'Invalid' : 'Valid' ; ?> </pre> matching numbers,characters, digits dont need regex we have ctype data to check this stuff if(ctype_alnum($testcase)){ echo 'valid'; }else{ echo 'invalid'; } php5 can also check email address and URL using predefined functions without using regex Quote Link to comment Share on other sites More sharing options...
etnastyles Posted January 16, 2008 Author Share Posted January 16, 2008 this dosnt check for werid freaky symbols like this || isnt preg_match therefore better??? ------------------------------code------------------------------- <?php $strings = array('1820.20', '10002', 'wsl!12'); foreach ($strings as $testcase) { if (ctype_digit($testcase)) { echo "The string $testcase consists of all digits.\n"; } else { echo "The string $testcase does not consist of all digits.\n"; } } Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 16, 2008 Share Posted January 16, 2008 this code $strings = array('1820.20', '10002', 'wsl!12','|| '); foreach ($strings as $testcase) { if (ctype_digit($testcase)) { echo "true $testcase <br/>"; } else { echo "false $testcase <br/>"; } } ?> gave me this output false 1820.20 true 10002 false wsl!12 false || whats wrong with that... I'm not saying don't use regex but dont over use regex if you only need to check numbers caracters etc.. you need php predefined functions i only use regex when i'm checking for a string on pattern 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.