Can someone help me with preg_match ?

Discussion in 'PHP' started by readezarchive, Aug 28, 2014.

  1. #1
    Hello I have the following preg_match:

    if (preg_match('~^[^0-9]~', $id)) {
    echo "<script>alert('no match found.')</script>";
    PHP:
    It is searching an ID which is made up of numbers like 84773

    The problem is, the preg_match still allows 84773+347.3253 for example.

    How do I make it only accept numbers like 23 and 39583?
     
    Solved! View solution.
    readezarchive, Aug 28, 2014 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Aug 28, 2014 IP
    ThePHPMaster likes this.
  3. readezarchive

    readezarchive Active Member

    Messages:
    600
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thank you for the reply.

    '~^\d+$~'
    PHP:
    This seems to still allow periods and + signs.

    I tried:
    if (!ctype_digit($id)) {
    echo "<script>alert('no match found.')</script>";
    Code (markup):
    Which gave an alert for an all number ID
     
    readezarchive, Aug 28, 2014 IP
  4. readezarchive

    readezarchive Active Member

    Messages:
    600
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Any one else have any ideas?

    I'm trying to give an alert if the ID is anything but a number like 83485 or 344 or 2. No periods, hyphens, plus signs or spaces, etc.

    Thanks!
     
    readezarchive, Aug 29, 2014 IP
  5. #5
    I tried with ctype_digit and it worked just fine, you could try is_numeric:
    if (!is_numeric($id)) {
    echo "<script>alert('no match found.')</script>";
    }
    PHP:
     
    malky66, Aug 29, 2014 IP
  6. readezarchive

    readezarchive Active Member

    Messages:
    600
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Turns out I was calling the wrong ID... they probably all work, but this one works just fine:

     
    readezarchive, Aug 29, 2014 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    is_numeric() accepts values like 0xffff. Not sure if this is a behaviour you're looking for.
     
    nico_swd, Aug 29, 2014 IP
  8. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #8
    Good point,
    @readezarchive ctype_digit() is probably the best way to go, you should reassign the best answer to nico_swd for post#2.
     
    malky66, Aug 30, 2014 IP
    ryan_uk likes this.
  9. readezarchive

    readezarchive Active Member

    Messages:
    600
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    I tested
    (!is_numeric($id)
    Code (markup):
    out with 34243x3434, 33423xffff and it worked correctly. Are you sure?

    The Url with the ID it's checking is like google.com/test.php?id=23435

    It's checking the ID to make sure it's all numbers with no space, letters or extra characters.
     
    readezarchive, Aug 30, 2014 IP