Strange behaviour with str_ireplace()

Strange behaviour with str_ireplace()

am 27.01.2010 00:02:15 von Angus Mann

------=_NextPart_000_00F1_01CA9F2F.6BF09A50
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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=3D34.5;
$message=3D"You owe me #amount#.";
$message1 =3D str_ireplace("#amount#,sprintf("%01.2f",$price),$message);
$message2 =3D =
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?


------=_NextPart_000_00F1_01CA9F2F.6BF09A50--

Re: Strange behaviour with str_ireplace()

am 27.01.2010 00:10:29 von Angus Mann

Grrr...please ignore.

After battling with this for a while I now can't reproduce the problem.

Apologies for an un-necessary post.

----- Original Message -----
From: "Angus Mann"
To: "PHP-General"
Sent: Wednesday, January 27, 2010 9:02 AM
Subject: [PHP] Strange behaviour with str_ireplace()


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?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php