SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot NevadaSam's Avatar
    Join Date
    May 2006
    Location
    Georgia
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace or something

    I want to remove everthing from a string except the numbers. The strings will be mostly like this, but won't always have letter at the end.

    $str = 'D-6430zip';

    For the above string, I want the echo results to = 6430

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $sString 
    'D-6430zip';
    echo 
    $sString#D-6430zip
    $sString preg_replace('~[^0-9]~'''$sString); #removes anything non-numeric
    echo $sString;#6430
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot NevadaSam's Avatar
    Join Date
    May 2006
    Location
    Georgia
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is just what I wanted.
    Thanks,
    Samantha

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Not a problem, HTH.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    can simplifiy the regex by just doing /\D/
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Zealot NevadaSam's Avatar
    Join Date
    May 2006
    Location
    Georgia
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like simple things... thanks logic_earth.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •