Forum Moderators: coopster
BUT - for visual presentation I want to enter a comma into all my prices. I personally feel that £1,298.54 looks much better than £1298.54, certainly more professional. I need to keep the size of coding right down so my current:
$a[example2] = str_replace("£1", '£1,', $a[example1]);
$a[example2] = str_replace("£2", '£2,', $a[example2]);
$a[example2] = str_replace("£3", '£3,', $a[example2]);
etc.
is pretty lengthy. Is there a command to enter characters at a certain point in the string? e.g. for the "thousands" is there coding to place a comma after the second character of the string?
What you have to do is to count the string length, and if there's a dot existing
<?PHP
if(!function_exists('addCommas'))
{
function addCommas($number)
{
//$number = "£1234.56"
$isdot = strpos($number, '.');if($isdot === false) $number = $number."00"; //£200 -> £200.00
elseif(!$isdot) return "£0".$number;//.50 -> £0.50
else
{$ispound = strpos($number, '£');
if($ispound === false) $number = "£".$number; //turns 200.98 into £200.98
$length = strlen($number);
if($length <= 7) return $number//£999.99 7 characters
else
{
$temp = substr($number, -7);
$rest = substr($number, $length - 8);
for($i = ceil(($length - 7)/3); $i > 0; $i--)// if x>1000, then
{
$temp = substr($rest, -3).",".$temp;
$rest = substr($rest, strlen($rest) - 4);
}
return $temp
}
}
}
}
?>
It looks horrible, but may work. Sorry, but I couldn't test it.
best regards!
Michal Cibor
PS. Do you know of a function, that changes date formats? Eg from MySQL format (YYYY-MM-DD) to postgreSQL format (DD MMM YYYY)?
I'd be grateful!