tycoon79 Posted January 24, 2009 Share Posted January 24, 2009 hi all..i need some help with php code i have this code $ValResult=0; preg_match_all('/^[a-zA-Z- .,]{2,200}$/', $Valstring, $matches); foreach($matches[0] as $value){ if($value!='' || $value!='0' || $value!='null'){ $ValResult=1; } else{ $ValResult=0; } } return $ValResult; and try it with 1)$Valstring='hulu'; and its return $ValResult=1--ok 2)$Valstring='hulu-'; and its return $ValResult=1--ok 3)$Valstring='hulu123'; and its return $ValResult=0--ok 4)$Valstring='hulu]'; and its return $ValResult=0--ok but the when i try it with 5)$Valstring='hulu+'; and its return $ValResult=1--not ok it suppose to return $ValResult=0 can someone help me with..thanking you all in advance for reviewing my post Quote Link to comment https://forums.phpfreaks.com/topic/142212-php-regex-code-problem-with-symbol/ Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 Your code returns a 0 on "hulu+" for me... anyways, you can try '/^[-a-zA-Z .,]{2,200}$/' Also, since you are expecting the entire string to consist of this match, you can use preg_match instead of preg_match_all and ditch the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/142212-php-regex-code-problem-with-symbol/#findComment-745014 Share on other sites More sharing options...
cwarn23 Posted January 24, 2009 Share Posted January 24, 2009 I have altered the script so that it will make the + symbol as 0. <? $ValResult=0; $Valstring='hulu+'; preg_match_all('/^[a-zA-Z- .,]{2,200}$/', $Valstring, $matches); foreach($matches[0] as $value){ if(!in_array($value,array('','0','null','+'))){ $ValResult=1; } else{ $ValResult=0; } } return $ValResult; ?> The way I altered the if function makes more sense. Quote Link to comment https://forums.phpfreaks.com/topic/142212-php-regex-code-problem-with-symbol/#findComment-745154 Share on other sites More sharing options...
nrg_alpha Posted January 24, 2009 Share Posted January 24, 2009 One thing immediately jumps out at me: /^[a-zA-Z- .,]{2,200}$/ When intending on using a dash character as a literal in a character class, always have it listed as either the very first (or very last) character, otherwise, this creates a range (not unlike 0-9).. so in that pattern, you are asking for a range from Z to space.. this creates unforseen problems. Notice how CV's version puts the literal dash as the very first character? This is the correct way. Quote Link to comment https://forums.phpfreaks.com/topic/142212-php-regex-code-problem-with-symbol/#findComment-745178 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 As I mentioned before, since you are trying to match the full string, you don't need preg_match_all. Also, since you're simply trying to figure out if it matched, and return a 1 or 0 based off that, all of that code can be condensed down to this single line: return preg_match('/^[-a-zA-Z .,]{2,200}$/', $Valstring); if $Valstring matches the regex, true (1) will be returned. If it doesn't, false (0) will be returned. Quote Link to comment https://forums.phpfreaks.com/topic/142212-php-regex-code-problem-with-symbol/#findComment-745217 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.