SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strip commas and spaces

    Hey everyone,

    Is there a native PHP function that strips commas, dots and spaces from a number?

    for example the number 1,000.25 should become 1000.25

    many thanks
    Andy

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm this doesn't seem to work. I have a variable "fl_area" that produces a text with a comma 1,025 but the function to convert it to feet needs the comma to be removed for the conversion to be successful.

    Code:
    metersToFeetInches( number_format(get_field( "fl_area" ), 2, '.', '') );

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by neodjandre View Post
    Hmm this doesn't seem to work. I have a variable "fl_area" that produces a text with a comma 1,025 but the function to convert it to feet needs the comma to be removed for the conversion to be successful.

    Code:
    metersToFeetInches( number_format(get_field( "fl_area" ), 2, '.', '') );
    Ah I see, try to make it a habit to store your "numbers" as such, without commas, you can always format then with them using number_format later.

    Code PHP:
    $string = '1,600.45';
    $number = (double)str_replace(",", "", $string);

    Code PHP:
    metersToFeetInches((double)str_replace(",", "", $fl_area));


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
  •