Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts

    RegEx to remove everything but numbers

    Hi,

    I need some RegEx that will take a string, for example .5 gal or 1.25 qts, and remove everything but the numbers from the string.

    Is this possible?

    thank you!

  • #2
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts
    PHP Code:
    $y=ereg_replace("[^0-9]"""$x); 

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by FuZion View Post
    Hi,

    I need some RegEx that will take a string, for example .5 gal or 1.25 qts, and remove everything but the numbers from the string.

    Is this possible?

    thank you!
    PHP Code:
    $result preg_replace("/\D/","",$phone); 
    regards

  • #4
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Will those keep the decimals as well?

    Thanks guys!

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    For decimals you can do this:

    PHP Code:
    $string 'fdsgd3.141asdggfgfdsdniojn';

    echo 
    preg_replace '#\D*?(\d+(\.\d+)?)\D*#''$1'$string );

    // Outputs 3.141 

  • #6
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Perfect. Thank you!

  • #7
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Well, it's working great except for one thing. If the number starts with a decimal, it doesn't keep the decimal. How do I fix this?

    I really appreciate your help!

  • #8
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    preg_replace '#\D*?(\d*(\.\d+)?)\D*#''$1'$string ); 

  • Users who have thanked Mwnciau for this post:

    FuZion (05-15-2008)

  • #9
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, well I tried that one, and it rounded my .5 up to one. Is there something on the server that I need to change?

    This is exactly what the string is:
    Code:
    .5 gal
    But, when I insert it into the database it changes it to 1 now. Any ideas?

    I really appreciate all the help, thank you!

  • #10
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Just a note, the string wont always be what I posed above, it could be 1.24 qts or 56.77 lbs.

  • #11
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Try this:

    PHP Code:
    $result=ereg_replace("[^.0-9]"""$x); 

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • #12
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Do not use ereg_replace(), it has been deprecated for several versions. PHP6 won't even contain the ereg_* functions, so it would be setting yourself up for failure to use them.

  • #13
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Thanks - I was not aware of that.

    Try this:

    PHP Code:
    <?php 

    $result
    =preg_replace("/[^0-9.]/"""$input);

    ?>
    Last edited by logictrap; 05-13-2008 at 07:57 PM. Reason: additional info

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • Users who have thanked logictrap for this post:

    FuZion (05-15-2008)

  • #14
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks guys I appreciate it!

  • #15
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You have to escape the decimal or it will be read as a wildcard.
    PHP Code:
    $input preg_replace'/[^\d\.]/'''$input ); 


  •  

    Posting Permissions

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