SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing spaces and commas

    Hi,
    I am trying to make it so when someone enters price like:
    500,000 or 500 000 or $500,000 or whatever it converts it to just:
    500000 so it's stored and searchable in the MySQL database.
    Can someone help me out?

  2. #2
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it:
    PHP Code:
    <?php
    $in 
    = array("$"" "",");
    $out = array("""""");

    $output str_replace($in$out$value);
    echo 
    $output;
    ?>
    $value is the submitted form number.





    Cheers.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks pretty simple,
    thanks.

    One more question.
    If I take the number 500000 from the database how can I convert it back to 500,000?

  4. #4
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use number_format():
    PHP Code:
    <?php
    $db_number*
    =*"500000";
    $output*=**number_format($db_number);

    echo 
    $ouput;
    ?>

    For more info check the manual




    Cheers.

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by filmfoto
    This should do it:
    PHP Code:
    <?php
    $in 
    = array("$"" "",");
    $out = array("""""");

    $output str_replace($in$out$value);
    echo 
    $output;
    ?>
    what happens if they enter something other than a space, ,, or $? such as "a"? this will eliminate anything but numbers:

    PHP Code:
    $txt preg_replace('/\\D+/'''$txt); 
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The preg_replace MASTER has spoken.



    Cheers.


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
  •