Jump to content

srry noob question


Monkuar

Recommended Posts

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

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.