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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/ 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? Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-439952 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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-439954 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> Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-439958 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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-439963 Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 Right. It's the ternary operator. Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-439986 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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-440000 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??? Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-440015 Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 \s is the shorthand for whitespace. Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-440017 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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-440406 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"; } } Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-441076 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 Link to comment https://forums.phpfreaks.com/topic/86149-preg_match-or-regx-finding-foreign-chrs-in-form-entrys/#findComment-441379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.