note 84430 added to function.str-replace
| From: | donriffel at gmail dot com | Date: | Mon, 14 Jul 2008 11:05:08 +0000 |
| Subject: | note 84430 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141473@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 209.41.74.194
Probable Submitter: 67.181.111.137
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/84430
Del: integrated -- https://master.php.net/note/delete/84430/integrated
Del: useless -- https://master.php.net/note/delete/84430/useless
Del: bad code -- https://master.php.net/note/delete/84430/bad+code
Del: spam -- https://master.php.net/note/delete/84430/spam
Del: non-english -- https://master.php.net/note/delete/84430/non-english
Del: in docs -- https://master.php.net/note/delete/84430/in+docs
Del: other reasons-- https://master.php.net/note/delete/84430
Reject -- https://master.php.net/note/reject/84430
Search -- https://master.php.net/manage/user-notes.php