Forum Moderators: coopster

Message Too Old, No Replies

Entering character at certain point in string ...

... can it be done avoiding str_replace?

         

internetheaven

10:06 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My database has a large number of prices. (e.g. £1339.95, £1298.54 etc.) each of which is separated according to size - i.e. the thousands are in one section, the hundreds in another etc.

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?

mcibor

10:36 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not that simple, because you assume, that all values are from 1000.00 to 9999.99, and that may not be true.

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

demnetia

5:02 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Wow, haven't you guys heard of number_format() or sprintf()?

[php.net...]
[php.net...]

mcibor

2:45 pm on Jan 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Demnetia for the info. I didn't know about the number_format() function. sprintf i know, but I don't use it - it would still require to split the number into threes.

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!

demnetia

3:46 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



There is no predefined function to that that I can think of, but you could create a custom one.
You could use [php.net...] on the MySQL date, then format the resulting timestamp using the other date functions. You might end up using regular expression or exploding the MySQL date into segments. I haven't tried it before, but it should be fairly easy.