Strange behaviour with str_ireplace()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all. I found some unexpected behaviour with str_ireplace.

To summarize, if the $replace term is a number, but formatted as currency with trailing zero's (34.50) the function drops the trailing zero.

Example :

$price=34.5;
$message="You owe me #amount#.";
$message1 = str_ireplace("#amount#,sprintf("%01.2f",$price),$message);
$message2 = str_ireplace("#amount#,"$".sprintf("%01.2f",$price),$message);


$message1 will yield "You owe me 34.4"
$message2 will yield "You owe me $34.40"

So in other words, if the replace term can be interpreted as a number, it will be, including truncation of zero's.
I didn't expect this, because as the function name suggests, it's a *string* replace.

Fortunately I can just add a $ to the amount and make it work as a string, not a number but this would not always be the case.

Is this by design, or is it a bug?


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux