HoTDaWg Posted March 12, 2007 Share Posted March 12, 2007 what would the code look like if you wanted to tell the user their info they submitted into a form had invalid characters? would you use ereg? thanks HoTDaWg Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/ Share on other sites More sharing options...
donynam Posted March 12, 2007 Share Posted March 12, 2007 use regular expression. Please check this page http://my2.php.net/manual/en/function.preg-match.php or you can write some classes or functions to handle this situation. Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205521 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 would you use ereg? As donynam suggests, I would use preg as well. Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205530 Share on other sites More sharing options...
HoTDaWg Posted March 12, 2007 Author Share Posted March 12, 2007 any idea why this jumps directly to the string acceptable instance? <?php $username ="ABCDEFGhjikl(*&*%$*())*^%$#!mnop102022\/.,[]]]]]]"; if (preg_match("[a-zA-Z0-9]",$username)){ echo "Unacceptable characters are in this string"; }else{ echo "string acceptable:S \r \n {$username} "; } ?> again, all i want to do is identify if the variable contains specific characters. Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205663 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 any idea why this jumps directly to the string acceptable instance? As you have it, you are matching any string that contains at least one alphanumeric character. If you are trying to find any strings that contain only alphanumeric characters, you'll want to do something like this: <?php $username ="ABCDEFGhjikl(*&*%$*())*^%$#!mnop102022\/.,[]]]]]]"; // negative match if (preg_match('|[^a-z\d]|i', $username)) { // not valid } // positive match if (preg_match('|^[a-z\d]+\z|i', $username)) { // valid match } ?> Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205680 Share on other sites More sharing options...
HoTDaWg Posted March 12, 2007 Author Share Posted March 12, 2007 at the risk of sounding noobish, i am curious to know why are the specific characters |[^a-z\d]|i placed within the single quotes? Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205685 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 at the risk of sounding noobish, i am curious to know why are the specific characters |[^a-z\d]|i placed within the single quotes? [pre] "|" == my choice of delimiter (opening) "[" == begin a character set "^" == declares a negative search (match anything NOT part of this set) "a-z" == alpha characters "\d" == digits "]" == close character set "|" == my choice of delimiter (closing) "i" == declares a case insensitive search [/pre] Hope this helps Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205688 Share on other sites More sharing options...
HoTDaWg Posted March 12, 2007 Author Share Posted March 12, 2007 very much so , thanks a lot obsidian Link to comment https://forums.phpfreaks.com/topic/42367-solved-if-statement-help-identifying-if-the-user-has-placed-invalid-characters/#findComment-205694 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.