Strange behaviour with str_ireplace()
| From: | Angus Mann | Date: | Tue, 26 Jan 2010 23:02:15 +0000 |
| Subject: | Strange behaviour with str_ireplace() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-301500@lists.php.net to get a copy of this message | ||
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?