I need to add 2 numbers together

Discussion in 'PHP' started by jc@ukzone.com, Oct 9, 2007.

  1. #1
    I currently use a script to get a number from my webstats.html file and then display it on a web page. I use analog to search the log files and create the webstats.html file. My log file went above two gig size and analog couldn't cope with that size.
    This number in the webstats.html file is on a line:

    <br><b>Successful requests:</b> 21,074 (14)

    This is the code that I use to get the number from webstats.html is:

    # get the webstats.html file
    $statfile="logs/webstats.html";
    $stats = join('',file($statfile));

    # get the total hits number
    $start=strpos($stats,"<br><b>Successful requests:</b>");
    $end=strpos($stats,"(",$start);
    $totalhits=substr($stats,$start,$end - $start);
    $start=strpos($totalhits,"/");
    $totalhits=substr($totalhits,$start+4);

    The output on the web page is:

    <? echo $totalhits ?>

    and that displays 21,074

    I now need to display the total hits PLUS the previous total hits as one figure.
    e.g.
    $previoustotalhits = "5,200,000";
    $totalhits = 21,074 the number from the webstats.html file

    I know that I add two strings together. but the numbers contained in the strings have to be without commas. e.g. <? echo $previoustotalhits + $totalhits ?> This doesn't work unless I remove the commas.
    It then gives an output of 5221074

    I need to remove the commas from the $totalhits number. Then add it to the $previoushits number.

    I would then like to add the commas back into the final number so that it makes it easier to read.
    e.g. 5,221,074

    Can anybody help - PLEASE
     
    jc@ukzone.com, Oct 9, 2007 IP
  2. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    $num=preg_replace("/[^0-9]+/","",$num);
     
    tandac, Oct 9, 2007 IP
  3. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks very much tandac.
    I have now got that to work, but I finish up with a number without commas.
    e.g. 20000 + 1149 returns 21149

    I would like the return to give 21,149

    Is this possible ???
     
    jc@ukzone.com, Oct 9, 2007 IP
  4. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Sure ;)
    Try it like this:
    
    $numberWithComma = number_format ( $number , 0 , '.', ',' );
    
    PHP:
     
    theOtherOne, Oct 9, 2007 IP
  5. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Many thanks to tandac and theOtherOne...
    You guys are wonderful.
    I now have what I am looking for.
    many thanks..

    John C
     
    jc@ukzone.com, Oct 9, 2007 IP