meltingpoint Posted July 12, 2011 Share Posted July 12, 2011 Greetings regex experts. I have an input form that assigns levels to each user. There are 7 total. I need to test the input so that it is loaded properly. The edit box entry should look like : 1,2,3,4,5,6,7 - it needs to be separated by commas and need to end with no comma. - it can be 1,2,3 or 1,4,7 or all of them or only one or two - the important thing is that it be 1-7, each separated by commas and end with no comma. Any help would be appreciated. Tks Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/ Share on other sites More sharing options...
AyKay47 Posted July 12, 2011 Share Posted July 12, 2011 $pattern = '~^([1-7,]{1,6}[1-7]{1})$~'; preg_match($pattern,$string,$matches); should work for you Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241631 Share on other sites More sharing options...
JAY6390 Posted July 12, 2011 Share Posted July 12, 2011 ~^[1-7](,[1-7])*$~ This will do it, but if you want to be sure, you're best off using something like the following. It will make sure each value is only used once <?php $input = '1,2,3,,7,7,7,7,8,9,10'; $vals = array_unique(explode(',', $input)); foreach($vals as $key => $val) { if($val < 1 || $val > 7 || (int)$val != $val) { unset($vals[$key]); } } $output = implode(',', $vals); echo $output; Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241635 Share on other sites More sharing options...
JAY6390 Posted July 12, 2011 Share Posted July 12, 2011 @AyKay47 I think you've misunderstood, your code allows things like 111,112 and 111,,,1 Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241637 Share on other sites More sharing options...
AyKay47 Posted July 12, 2011 Share Posted July 12, 2011 must have misunderstood... Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241640 Share on other sites More sharing options...
meltingpoint Posted July 12, 2011 Author Share Posted July 12, 2011 Thanks guys- back from my coffee break. I will give it a try. Much thanks- Andy Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241650 Share on other sites More sharing options...
meltingpoint Posted July 12, 2011 Author Share Posted July 12, 2011 Ok- entering a permission level of 1 - does not work entering a permission level of 1,2 or 1,2,3 or 1,2,3,4 works fine entering a permission level of 1,2,3,4,5 does not work nor does 1,2,3,4,5,6 or 1,2,3,4,5,6,7 entering a permission level of 2,3,4,5 works but 2,3,4,5,6 does not So to recap- a person will have at least 1 permission level 1-7. And can have any combination of all 7 permission levels i.e. 1,3,5,7 or 2,4,5 etc..... Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241663 Share on other sites More sharing options...
Psycho Posted July 12, 2011 Share Posted July 12, 2011 I have a suspicion that you are really trying to do this the wrong way. Are you storing the value of "1,2,3" as a value into the database? Besides, if there are 7 possible values, just include 7 checkboxes for input (named as an array). That would make a much more elegant solution. Then just implode the POST values Form: Level 1 <input type="checkbox" name="levels[]" value="1' /> Level 2 <input type="checkbox" name="levels[]" value="2' /> Level 3 <input type="checkbox" name="levels[]" value="3' /> ...etc. PHP $levels = implode(','$_POST['levels']); however, here is a solution for you. The one thing it does not do is prevent a user from entering the same value twice (as long as the value is between 1-7), but it does ensure it matches the pattern you specified and does not contain more than seven digits. function checkLevels($levelsString) { return (preg_match("#^[1-7]{1}(,[1-7]{1}){0,6}$#", $levelsString)!==0); } //Testing array $testvalues = array( "1", "1,2", "11,3,4", "1,2,,4", "1,2,", "1,2,3,4,5,6,7", "", "1,2,3,8", "1,2,3,4,5,6,7,7" ); //Test process foreach($testvalues as $value) { $response = (checkLevels($value)) ? 'Pass' : 'Fail'; echo "[{$value}]: {$response}<br>\n"; } Test results [1]: Pass [1,2]: Pass [11,3,4]: Fail [1,2,,4]: Fail [1,2,]: Fail [1,2,3,4,5,6,7]: Pass []: Fail [1,2,3,8]: Fail [1,2,3,4,5,6,7,7]: Fail Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241678 Share on other sites More sharing options...
meltingpoint Posted July 12, 2011 Author Share Posted July 12, 2011 Exactly what was needed. Much thanks. (....i hope my hair grows back.....) Andy Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241693 Share on other sites More sharing options...
xyph Posted July 12, 2011 Share Posted July 12, 2011 Why not store the access levels in a bitwise method? http://www.litfuel.net/tutorials/bitwise.htm 1 - 1 2 - 2 3 - 4 4 - 8 5 - 16 6 - 32 7- 64 So someone who's 1,3,5 would be 1+4+16 = 21. You can check if someone has level 5 by doing If( $accessInteger & 16 ) { } The website does a great job of explaining what bitwise is and gives specific examples relevant to this discussion Storing in int rather than varchar saves you a TON of database space as well, and makes things faster Quote Link to comment https://forums.phpfreaks.com/topic/241752-need-test-specific-format-123/#findComment-1241868 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.