remove whitespace to allow adding in php4
Posted: Sat Dec 20, 2008 5:58 pm
I put together a script that that will allow for the adding of a string of numbers in a txt file. However, I've got a problem since the numbers have spaces in place of commas (ie 2 345 instead of 2,345). This causes an obvious problem when adding. I am therefore trying to remove the spaces. I considered the trim commands but they dont address spaces in the middle of numbers.
I tried the str_replace command but it doesnt seem to work (or Im doing it wrong).
Can someone help me add to this script to remove the spaces to thereby allow for the adding?
<?php
$lines = file('textfile.txt/');
echo "sum($lines) = " . array_sum($lines) . "\n";
$lines = file('textfile.txt');
$sum = array_sum($lines);
$fp = fopen('total.txt', 'w');
fwrite($fp, $sum);
fclose($fp);
?>
I tried the str_replace command but it doesnt seem to work (or Im doing it wrong).
Can someone help me add to this script to remove the spaces to thereby allow for the adding?
<?php
$lines = file('textfile.txt/');
echo "sum($lines) = " . array_sum($lines) . "\n";
$lines = file('textfile.txt');
$sum = array_sum($lines);
$fp = fopen('total.txt', 'w');
fwrite($fp, $sum);
fclose($fp);
?>