Strip

Discussion in 'PHP' started by Silver89, Feb 12, 2007.

  1. #1
    Ok, i have this

    $number

    which would equal a date in this format 2007-02-13

    What i am trying to do is remove the - and 0's from the date so it would equal

    27213

    This is what i have but its not working:

    $number = number_format($number);
    $number = strpos($number, "0") ;

    Please advise
     
    Silver89, Feb 12, 2007 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not use str_replace?

    $number = str_replace( '-', '', $number );
    $number = str_replace( '0', '', $number );
     
    TwistMyArm, Feb 12, 2007 IP
    Silver89 likes this.
  3. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #3
    <?php
    $number = "2007-02-13";
    $number = str_replace("0", "", $number);
    $number = str_replace("-", "", $number);
    echo $number; // result "27213"
    ?>

    good luck
     
    grandpa, Feb 12, 2007 IP
    Silver89 likes this.
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    thanks for your help! :)
     
    Silver89, Feb 12, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    FYI you can use arrays for str_replace, makes slightly more efficient code ...

    
    $number = str_replace( array("0", "-"), array("", ""), $number );
    
    PHP:
     
    krakjoe, Feb 13, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Yet on another sidenote, the second argument doesn't need to be an array. If it's a string all items in the first array will be replaced with it. So this would work too.
    
    $number = str_replace( array("0", "-"), "", $number );
    
    PHP:
     
    nico_swd, Feb 13, 2007 IP