strip out non numbers using php

Discussion in 'PHP' started by Astroman, Apr 11, 2009.

  1. #1
    How can I make this query strip out any extra form inputs that aren't numbers for the $_POST[price] ?

    Can I alter the below code or is it better to put something in the form input instead? I just want to make sure they only enter positive numbers, so they can't put . or -

    $add = doquery("INSERT INTO {{table}} VALUES ('', '$userrow[id]', '$type', '$item', '$_POST[price]';
    Code (markup):
     
    Astroman, Apr 11, 2009 IP
  2. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use this PHP code to strip each text except for Numbers:
    
    $numbers=ereg_replace("[^0-9]", "", $string);
    
    PHP:
    Thanks,
    hope it helps!
     
    mehdi, Apr 11, 2009 IP
  3. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Thanks, how would I write that in here:

    <input type='hidden' name='price' value='$_POST[price]' />
     
    Astroman, Apr 11, 2009 IP
  4. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $numbers=ereg_replace("[^0-9]", "", $_POST['price']);
    echo '<input type="hidden" name="price" value="'.$numbers.'" />';
    
    PHP:
     
    mehdi, Apr 11, 2009 IP
    Astroman likes this.
  5. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Thanks, rep added. I couldn't get it to work in the form like that but I used what you showed me like this:

    $goodcleannumbers=ereg_replace("[^0-9]", "", $_POST['price']);
    			$add = doquery("INSERT INTO {{table}} VALUES ('', '$userrow[id]', '$type', '$item', '.$goodcleannumbers.',...
    Code (markup):
    Does that look right, it seems to work?
     
    Astroman, Apr 12, 2009 IP
  6. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, looks right to me.. have a test
     
    mehdi, Apr 12, 2009 IP