Monkuar Posted May 18, 2012 Share Posted May 18, 2012 But how do I check input to make sure that the input has to be seperated by commas and has to be a integer like "1,2,3,4,5" is correct if a user puts "1,:25,,l2@" or something funky, I can error out Thanks also is there a php function that does this already? and is this way safe? im trying to store comma seperated id's Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/ Share on other sites More sharing options...
trq Posted May 18, 2012 Share Posted May 18, 2012 Where are you trying to store comma separated id's? If it's a database, why? Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346570 Share on other sites More sharing options...
scootstah Posted May 18, 2012 Share Posted May 18, 2012 Do you want to just strip characters out that aren't integers or commas, or give the user feedback? Strip out any characters that are not integers or commas: $input = '1,:25,,l2@'; $input = preg_replace('/([^\d,])+/', '', $input); // we might end up with two or more commas next to eachother, so filter that out $input = preg_replace('/(,{2,})+/', '', $input); Only accept the format: 1,2,3,4,5 $input = '1,2,3,4,5'; if (!preg_match('/^\d+(,{1}\d)*$/', $input)) { echo 'invalid input'; } Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346572 Share on other sites More sharing options...
Monkuar Posted May 18, 2012 Author Share Posted May 18, 2012 Where are you trying to store comma separated id's? If it's a database, why? Because I have forums where I only want a special link to show if the forum id number matches what's in a global value. so if forum id is like 26, and my global value is 2,5,27,21,26 and if it matches then I want it to show a special link for that specific forum (If it's a trading forum to be exact, I need to show a "Request a Mediator Link") so people can get help if they're trading or need to trade or need someone for help so I think it's easier if I just use the in_array function to check global id's and if the forum id is in there, then it will show the link, i think that's the fastest way? i might be wrong though let me check scoot's code and see what i can do Okay, this one works the best for me $input = '1,2,3,4,5'; if (!preg_match('/^\d+(,{1}\d)*$/', $input)) { echo 'invalid input'; } Thank you scootsa, I will jsut error out to the user if they indeed are trying to be nawty. Should I escape this too or it's fine? Im trying to learn not just steal code, so.. on this one $input = preg_replace('/([^\d,])+/', '', $input); Where does this show so it only accept's integers? I looked on how to find to match integers only.. http://stackoverflow.com/questions/9043551/regex-match-integer-only Looks like this does it ^\d+$ In your code u have the ^\d part, why is there a +$ sign? for what reason, makes it confusing. I dont want this topic to be moved to the regex forum, I just was wondering that. I looked at google and regex tutorials, where is a good place to start? https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=regex+tutorial+detect+integer+only I tried this but cant find anything that would make it so I can detect integers only and strip them, like what you did. Thank you Im just trying not to just copy code from you guys like I used to, I want to actually learn this crap so I can ask less questions which I have been doing.. I think alot of people take advantage of this forum and dont realize the help they receive.. And I know I can be to quick to post sometimes, but it's just because I am really excited about what I am coding, or have no idea what im talking about Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346575 Share on other sites More sharing options...
scootstah Posted May 18, 2012 Share Posted May 18, 2012 Where does this show so it only accept's integers? I looked on how to find to match integers only.. http://stackoverflow.com/questions/9043551/regex-match-integer-only Looks like this does it ^\d+$ Yes, the \d is a character class which means "digit". It is the same as [0-9]. In your code u have the ^\d part, why is there a +$ sign? for what reason, makes it confusing. The + means "match 1 or more of this pattern". The $ marks the end of the string. If that wasn't there, it would ignore anything after the pattern is matched as long as the pattern was matched once. So 1,2asdfsafaf would be valid. I looked at google and regex tutorials, where is a good place to start? I find this cheat sheet to be a good reference: http://www.addedbytes.com/cheat-sheets/download/regular-expressions-cheat-sheet-v2.png Also, this site: http://www.regular-expressions.info/tutorial.html Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346576 Share on other sites More sharing options...
Monkuar Posted May 18, 2012 Author Share Posted May 18, 2012 OKay, problem $input = '1,2,3,4,5,6,2'; if (!preg_match('/^\d+(,{1}\d)*$/', $input)) { echo 'invalid input'; } Works, but let's say a number that is 2integers long or higher? $input = '1,2,3,4,5,6,2521,231'; if (!preg_match('/^\d+(,{1}\d)*$/', $input)) { echo 'invalid input'; } I changed if (!preg_match('/^\d+(,{1}\d)*$/', $input)) { to if (!preg_match('/^\d+(,{4}\d)*$/', $input)) { Because 2521 is 4 numbers long, but it still doesn't work am I missing something here? Doesn't {} represent how long the integer can be? Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346586 Share on other sites More sharing options...
scootstah Posted May 18, 2012 Share Posted May 18, 2012 This will work: ^\d+(,{1}\d+)*$ Quantifiers need to be placed to the right of the subject. You are modifying the quantifier for the comma, not the digit. Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346591 Share on other sites More sharing options...
Monkuar Posted May 18, 2012 Author Share Posted May 18, 2012 Okay, Topic Solved, This was awesome, I actually learned some regex here Quote Link to comment https://forums.phpfreaks.com/topic/262729-srry-noob-question/#findComment-1346593 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.