<?php

// 解析固定宽度字段数据记录 unpack
function fixed_width_unpack($format_str, $data)
{
    $arr = array();
    for ($i = 0, $j = count($data); $i < $j; $i++) {
        $arr[$i] = unpack($format_str, $data[$i]);
    }
    return $arr;
}
$books = array('Elmer Gantry             	Sinclair Lewis	1927', 'The Java Programing      	Yangc Bruce   	1997', 'Angular Js               	Google Project	2013', 'The Parsifal Mosaic      	William Styrom	1979');
$res = fixed_width_unpack('A25title/A15author/A5time', $books);
echo "<pre>";
print_r($res);
<?php

$book_array = fixed_width_unpack('A25title/A15author/A4publication_year', $books);
print "<table>\n";
// print a header row
print '<tr><td>';
print join('</td><td>', array_keys($book_array[0]));
print "</td></tr>\n";
// print each data row
foreach ($book_array as $row) {
    print '<tr><td>';
    print join('</td><td>', array_values($row));
    print "</td></tr>\n";
}
print "</table>\n";
Esempio n. 3
0
    $title = str_pad(substr($book[0], 0, 25), 25, '.');
    $author = str_pad(substr($book[1], 0, 15), 15, '.');
    $year = str_pad(substr($book[2], 0, 4), 4, '.');
    print "{$title}{$author}{$year}" . PHP_EOL;
}
function fixed_width_substr($fields, $data)
{
    $r = array();
    for ($i = 0, $j = count($data); $i < $j; $i++) {
        $line_pos = 0;
        foreach ($fields as $field_name => $field_length) {
            $r[$i][$field_name] = rtrim(substr($data[$i], $line_pos, $field_length));
            $line_pos += $field_length;
        }
    }
    return $r;
}
$book_fields = array('title' => 25, 'author' => 15, 'publication_year' => 4);
$book = array('Elmer GantryElmer GantryE Gantry Sinclai 1927', 'Elmer GantryElmer GantryE Gantry Sinclai 1937', 'Elmer GantryElmer GantryE Gantry Sinclai 1947');
print_r(fixed_width_substr($book_fields, $book));
print PHP_EOL;
function fixed_width_unpack($format = 'A25title/A15author/A4publication_year', $data)
{
    $r = [];
    for ($i = 0, $j = count($data); $i < $j; $i++) {
        $r[$i] = unpack($format, $data[$i]);
    }
    return $r;
}
print_r(fixed_width_unpack('A25title/A15author/A4publication_year', $book));
echo PHP_EOL;
Esempio n. 4
0
$booklist = <<<END
Elmer Gantry             Sinclair Lewis 1927
The Scarlatti InheritanceRobert Ludlum  1971
The Parsifal Mosaic      Robert Ludlum  1982
Sophies Choice           William Styron 1979
END;
function fixed_width_unpack($format_string, $data)
{
    $r = array();
    for ($i = 0, $j = count($data); $i < $j; $i++) {
        $r[$i] = unpack($format_string, $data[$i]);
    }
    return $r;
}
$books = explode("\n", $booklist);
$book_array = fixed_width_unpack('A25Title/A15Author/A4Publication_year', $books);
// Print as Table:
print "<table>\n";
// print a header row
print '<tr><td>';
print join('</td><td>', array_keys($book_array[0]));
print "</td></tr>\n";
// print each data row
foreach ($book_array as $row) {
    print '<tr><td>';
    print join('</td><td>', array_values($row));
    print "</td></tr>\n";
}
print "</table>\n";
?>