note 42127 deleted from function.sscanf by nlopess
| From: | nlopess@php.net | Date: | Tue, 04 May 2004 13:29:00 +0000 |
| Subject: | note 42127 deleted from function.sscanf by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69205@lists.php.net to get a copy of this message | ||
Note Submitter: seweso
----
<?php
// Want to do this:
$date = mktimewithformat ($_GET['date'], "«day:%2d»-«month:%2d»-«year:%4d»
«hour:%2d»:«minute:%2d»");
// Use these functions:
function sscanfnamed ($str, $format, $startochar = "«", $endochar = "»") {
$exp = explode ($endochar, $format);
$aformat = "";
$keys = array();
foreach ($exp as $piece) {
$test = explode($startochar, $piece);
// Test to see if this is the last piece
if (isset($test[1])) {
list($prefix, $piece) = $test;
list($keyname, $format) = explode(":", $piece);
$aformat .= $prefix . $format;
$keys[] = $keyname;
} else {
//add last piece of the format-string
$aformat .= $piece;
}
}
// Parse str
$result = sscanf($str, $aformat);
if (!is_array($result)) {
return array();
}
$resultnamed = array();
foreach($result as $key => $value) {
$resultnamed[$keys[$key]] = $value;
}
return $resultnamed;
}
function mktimefromarray($array) {
return mktime ( $array['hour'],
$array['minute'],
$array['second'],
$array['month'],
$array['day'],
$array['year']);
}
function mktimewithformat ($datestr, $format) {
return mktimefromarray(sscanfnamed($datestr, $format));
}
?>