note 93714 added to function.unpack
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 24 Sep 2009 03:02:39 +0000 |
| Subject: | note 93714 added to function.unpack | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161004@lists.php.net to get a copy of this message | ||
Functions I found useful when dealing with fixed width file processing, related to unpack/pack
functions.
<?php
/**
* funpack
* format: array of key, length pairs
* data: string to unpack
*/
function funpack($format, $data){
foreach ($format as $key => $len) {
$result[$key] = trim(substr($data, $pos, $len));
$pos+= $len;
}
return $result;
}
/**
* fpack
* format: array of key, length pairs
* data: array of key, value pairs to pack
* pad: padding direction
*/
function fpack($format, $data, $pad = STR_PAD_RIGHT){
foreach ($format as $key => $len){
$result .= substr(str_pad($data[$key], $len, $pad), 0, $len);
}
return $result;
}
?>
----
Server IP: 202.89.58.1
Probable Submitter: 203.97.178.3
----
Manual Page -- http://www.php.net/manual/en/function.unpack.php
Edit -- https://master.php.net/note/edit/93714
Del: integrated -- https://master.php.net/note/delete/93714/integrated
Del: useless -- https://master.php.net/note/delete/93714/useless
Del: bad code -- https://master.php.net/note/delete/93714/bad+code
Del: spam -- https://master.php.net/note/delete/93714/spam
Del: non-english -- https://master.php.net/note/delete/93714/non-english
Del: in docs -- https://master.php.net/note/delete/93714/in+docs
Del: other reasons-- https://master.php.net/note/delete/93714
Reject -- https://master.php.net/note/reject/93714
Search -- https://master.php.net/manage/user-notes.php