SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast factorypolaris's Avatar
    Join Date
    Oct 2004
    Location
    Big Lake, MN
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove Commas From Numbers

    I need to remove commas from numbers and I cant find the function that does this. I know I have used it before Somewhere but I cant remember.

    PHP Code:
    $number 45,900;

    somefunction($number); // output: 45900 
    Thanks for your help.
    Its a great day to be frustrated.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    str_replace(",", "", $number)
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast factorypolaris's Avatar
    Join Date
    Oct 2004
    Location
    Big Lake, MN
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You. WOrks Great
    Its a great day to be frustrated.

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    str_replace(",", "", $number)
    Thumbs up

  5. #5
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys,

    str_replace() works not as expected for me.
    Code:
    $bad_symbols = array(",", ".");
    $value = str_replace($bad_symbols, "", $value);
    Everything after comma or decimal point in $value gets removed, so that "23.45" and "23,45" become "23". I need them to become "2345".

    What am I doing wrong?
    Thanks!
    Algirdas

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    hi Algirdas,
    Welcome to the forums!

    When I run your code it gives me the expected result:
    PHP Code:
    <?php
    $value
    ='23.45';
    $bad_symbols = array(","".");
    $value str_replace($bad_symbols""$value);


    echo 
    $value;
    ?>
    outputs:
    Code:
    2345
    How do you get your $value ?

    Cheers

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Hey!
    Thanks for a prompt reply!

    Well, I'm trying to check if the user entered an integer. First, the data is received from the following form:
    Code:
    echo "<table><form name=\"IS\" action=\"insert.php?id=2\" method=\"post\">\n";					//load stored data in the table
    echo "<tr><td>Revenues:</td><td><input type=\"text\" name=\"revenues\" value=\"".$row['revenues']."\"></td></tr>\n";
    ...
    echo "<tr><td><input type=\"submit\" value=\"Update\"></td></tr>\n";
    echo "</form></table>\n";
    Then the submitted data is checked:
    Code:
    if (isset($_POST['revenues']) || isset($_POST['cogs']) || isset($_POST['ebitda']) || isset($_POST['ebit']) || isset($_POST['ebt']) || isset($_POST['net_income']))			//if any data has been submitted
    
    foreach ($_POST as $value)
    {
       $bad_symbols = array(",", ".");
       $value = str_replace($bad_symbols, "", $value);
       if (!empty($value) && $value !== strval(intval($value)))
          $error[$i++] = $value;
    }
    The comma/point is not registered as an error if I use str_replace(), so the data is entered into the database:
    Code:
    $query = "UPDATE `financials` SET revenues='".$_POST['revenues']."', cogs='".$_POST['cogs']."', ebitda='".$_POST['ebitda']."', ebit='".$_POST['ebit']."', ebt='".$_POST['ebt']."', net_income='".$_POST['net_income']."' WHERE company_id='".$_SESSION['company_id']."' AND term_id='".$_SESSION['term_id']."'";
    $result = mysql_query($query) or die ("<p class=\"error\">Error has occured while updating the database. Please try again.</p>");
    echo "<p>Database has been updated successfully.</p>";
    In the database, it's already "23" and not "2345".

    Thanks in advance,
    Algirdas

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    OK, what you are doing is checking for an error and replacing the , or . but then using the original $_POST data in the query - so it's never replaced in the query!

    It is better to check that the str_replace function is used on the individual $_POST entity and then register them as variables.

    So:

    PHP Code:
    $revenues str_replace($bad_symbols""$_POST['revenues']);
    //etc

    //and then use the variables in the query
    $query "UPDATE `financials` SET revenues='".mysql_real_escape_string($revenues)."',
    // etc 
    Hope you understand that,

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does str_replace take an array as an argument?

  10. #10
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Does str_replace take an array as an argument?
    Have you thought to look in the manual?
    str_replace

  11. #11
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    OK, what you are doing is checking for an error and replacing the , or . but then using the original $_POST data in the query - so it's never replaced in the query!
    So how does foreach() exactly work? I thought the "as" statement works analogously to passing by reference, I mean if I say foreach($smth as $smth2), then whatever I do to $smth2, the same is done to $smth, no? If so, then $_POST in my case should be amended properly. If not, then why doesn't the step strval(intval($value)) work, i.e. doesn't give an error since 23,45 is not a proper integer?

    Finally, how come the number is cut down to an integer - which part of the code does that (it is actually cut, because if the initial value is 23 and I enter 12,34, the new value becomes 12)?

    Thanks a lot! I feel like I'm learning a lot here..
    Cheers,
    Algirdas

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foreach actually creates a copy of your array, although you can optionally use references with it starting in php5.

    http://www.php.net/foreach

    although you could modify the $_POST array if you wanted to, i wouldnt recomend it. generally, its a good practice to leave the superglobals like $_POST etc.. alone and just read from them. if you need to change the values, create a new variable to store the changed value like spike showed you.

  13. #13
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    foreach loops through a copy of an array and the values are copied to the variables too. So any changes you make for these values do not reflect in the original array.

    intval() will not give you an error, it will return the integer value. It parses the first numeric characters, so everything after "," is omitted.

    The number is cut down to an integer in the database. You probably have an integer type, so it's the same as intval(), the integer value is parsed.
    Saul

  14. #14
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon
    foreach loops through a copy of an array and the values are copied to the variables too. So any changes you make for these values do not reflect in the original array.
    It's the other way around. The copy is worked on by the foreach loop but changes are applied to the source array. That means if you make changes to $array[2] while working on $array[1] those changes will not be apparent from the foreach loop once $array[2] is reached.

  15. #15
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Ok, now I'm trying it differently:
    Code:
    $new_data = $_POST;
    $bad_symbols = array(",", ".");
    for ($i = 0; $i <= count($new_data); $i++)
    {
       $new_data[$i] = str_replace($bad_symbols, "", $new_data[$i]);
       if (!empty($new_data[$i]) && $new_data[$i] !== strval(intval($new_data[$i])))
    	$error[$i++] = $new_data[$i];
    }
    When I run the file, it offers me to download an empty file with the same title as the one I'm running. If I comment the str_replace line, everything works as earlier, i.e. everything after the comma gets removed. Could anyone pls explain what's going on?

    Thanks a lot!
    Algirdas

  16. #16
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like, that this new script would never pass through $_POST['revenues']

  17. #17
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Hm, weird things are happening here. This code works fine (doesn't remove commas of course):
    Code:
    if (!empty($_POST))												{
       $new_data = $_POST;
       //$bad_symbols = array(",", ".");
       for ($i = 0; $i <= count($new_data); $i++)
       {
          //$new_data[$i] = str_replace($bad_symbols, "", $new_data[$i]);
          if (!empty($new_data[$i]) && $new_data[$i] !== strval(intval($new_data[$i])))
          $error[$i++] = $new_data[$i];
       }
    If I uncomment the two lines, I am either offered to download an empty .php file with the same filename as the file with this code (???) or I am told that this page can't be found (although when I access other parts of the same page, it's shown perfectly).

    What's going on?? Why are these two seemingly simple lines creating so many problems?

    Thanks,
    Algirdas

  18. #18
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    turn on error reporting. it will help you find your problems.
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    you have an endless loop is your problem. you keep recounting the $new_data array, which you keep making bigger each time you loop. it will never end.

    in addition, your _POST array likely has string keys, not numeric, so you wouldnt want to use a for() loop.
    a foreach loop would be easier.

    foreach ($array as $key => $value) {...)

  19. #19
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Bremen, Germany
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works!! Thanks a LOT guys! What a great feeling it is when the code works as expected...

  20. #20
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    glad you got it sorted algirdas
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •