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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    18
    Thanked 0 Times in 0 Posts

    PHP Math Help(Decimal Places)

    Okay I want to allow users to input numbers, I am currently using this to make sure there are no spaces or letters:

    PHP Code:
    $Apreg_replace("/[^0-9]/","",$A); 
    I want to allow users to input decimal places. I tried using this:

    PHP Code:
    $Apreg_replace("/[^0-9]+\.[0-9]/","",$A); 
    But it seems to be allowing letters through again.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    this seems to work:

    PHP Code:
    preg_replace("/[^0-9.]/","",$string); 

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Use
    '/^\d+(\.\d+)?$/'

    @angst - yours will work, but it will allow all sorts, such as
    .2236.246.234....................
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Regular Coder
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    18
    Thanked 0 Times in 0 Posts
    @Jay - I've tried to use what you've shown in your example, but it returns an null value every time.

    PHP Code:
    $Apreg_replace("/^\d+(\.\d+)?$/","",$A); 

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Oh I see, sorry I thought you were trying to match the string as a valid number
    Use this
    PHP Code:
    $Apreg_replace("/^.*?(\d+(\.\d+)?).*$/","$1",$A); 
    That should do it
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    Regular Coder
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I've gotten it working somewhat as:

    PHP Code:
    $Bpreg_replace("/[^\d+](\.\d+)?$/","",$B); 
    But my main problem is if I put letters before the numbers it doesn't take the letters out, for example I input 3.w3 I get 3.

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    can you give an example of the input string
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    Regular Coder
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    18
    Thanked 0 Times in 0 Posts
    3.w2 would be an input( a bad one that I'm trying to filter out), it should appear as 3.2, but the "w" sticks and PHP will treat it as a 3.

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    oh I see. this should work. Note that I had to use two regexes just in case there are any periods either side of the number after the letters are removed
    PHP Code:
    $a '..asdf.sdf.wfef.3.wsdf2.fewf.wef';
    $a preg_replace('/[^\d\.]+/'''$a);
    $apreg_replace("/^.*?(\d+(\.\d+)?).*$/","$1"$a);
    echo 
    $a
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    kochier (01-08-2010)


  •  

    Posting Permissions

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