note 84430 deleted from function.str-replace by mgf
| From: | mgf@php.net | Date: | Mon, 14 Jul 2008 15:03:37 +0000 |
| Subject: | note 84430 deleted from function.str-replace by mgf | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141491@lists.php.net to get a copy of this message | ||
Note Submitter: donriffel at gmail dot com
Reason: in docs
----
Be warned: While coding a Time class that utilizes the PEAR Date object, I came across some odd
behavior. Apparently the array functionality of strreplace() will overwrite previously replaced
values from the same function call.
In example, if an array (as shown below) has one value, say 'j' which is to be replaced by
'%e' and later in the same array the value 'e' is specified to be replaced by
'', the final resulting string will contain '%' only, without the 'e'.
Example parameter and return values below are followed by the quirky code...
Passed value: "Y-n-j G:i:s"
Expected result: "%Y-%m-%e %H:%M:%S"
Actual result: "%Y-%m-% %%H:%M:%S"
<?php
/**
* Converts a PHP date() formatted time string to the format used by the PEAR Date class.
* (Takes notation found in http://us.php.net/manual/en/function.date.php
and turns into PEAR Date object notation).
*
* @param string $sFormat
* @return string
*/
private function _dateObjFormatting($sFormat)
{
$preSyntax = array("d", "D", "j", "l", "N",
"S", "w", "z", "W", "F", "m",
"M", "n", "t", "L", "o", "Y",
"y", "a", "A", "B", "g", "G",
"h", "H", "i", "s", "u", "e",
"I", "O", "P", "T", "Z", "c",
"r", "U");
$postSyntax = array("%d", "", "%e", "%A", "",
"", "%w", "%j", "%U", "%B", "%m",
"", "%m", "", "", "", "%Y",
"%y", "%p", "%P", "", "%I", "%H",
"%I", "%H", "%M", "%S", "", "",
"", "%o", "%O", "", "", "%D",
"", "");
return = str_replace($preSyntax, $postSyntax, $sFormat);
}
?>
I'm sure most typical strreplace() arrays are not this long or complex. But in any event, this
info may save someone else a bit of headache and cursing.