[Zend Framework] Zend_Text: テキストの表を作成する
Zend_Text_Table を利用して、表をテキストで表示します。
plain text table とか ascii text table と呼ばれているようです。
[markdown]
結果から
[][1]
[1]: https://www.d-wood.com/wpmt/wp-content/uploads/2013/07/Zend_Text-2013-07-26-15-01-25.png
## Zend_Text_Table
> * [Zend_Text_Table(日本語) – Zend_Text – Zend Framework][2]
> * [Zend Framework – Zend_Text_Table promted to trunk][3]
> * [Zend Framework2.1.4 ZendTextTable – 弱小PHPerの憂鬱][4]
[2]: http://framework.zend.com/manual/1.12/ja/zend.text.table.introduction.html
[3]: http://zend-framework-community.634137.n4.nabble.com/Zend-Text-Table-promted-to-trunk-td653714.html
[4]: http://yuubiseiharukana.blog.shinobi.jp/Entry/1170/
>
“`php
public function textTableAction()
{
$data = array(
array(‘title’ => ‘路傍の石’, ‘author’ => ‘山本有三’, ‘pub’ => ‘1937’),
array(‘title’ => ‘羅生門’, ‘author’ => ‘芥川龍之介’, ‘pub’ => ‘1915’),
array(‘title’ => ‘城の崎にてa思い出して走る城の崎にて思い出して走る’, ‘author’ => ‘志賀直哉’, ‘pub’ => ‘1917’)
);
$this->view->assign(‘data’, $data);
// Text Table を作成する
$options = array(
‘columnWidths’ => array(20, 16),
‘AutoSeparate’ => Zend_Text_Table::AUTO_SEPARATE_HEADER,
// ‘decorator’ => ‘Ascii’,
‘padding’ => 1,
);
$table = new Zend_Text_Table($options);
// $table->setOutputCharset(‘ISO-2022-JP’);
$table->setInputCharset(‘utf-8’);
$row = new Zend_Text_Table_Row();
// $table->appendRow(array(‘Title’, ‘Sum. (hour)’));
$row->appendColumn(new Zend_Text_Table_Column(‘Title’));
$row->appendColumn(new Zend_Text_Table_Column(‘Sum. (hour)’, ‘right’));
$table->appendRow($row);
foreach ($data as $value) {
$row = new Zend_Text_Table_Row();
$row->appendColumn(new Zend_Text_Table_Column(“$value[title]”));
$row->appendColumn(new Zend_Text_Table_Column(“$value[author]”, ‘right’));
$table->appendRow($row);
}
$this->view->assign(‘table’, $table);
“`
* 横幅の指定が必須
* 全角文字があると罫線がずれる
## ArrayToTextTable
array-to-texttable.php を利用してみました。
> * [PHP: Array to Text Table Generation Class Multibyte][5]
> * [PHP Array to Text Tables][6]
[5]: https://gist.github.com/Thingmand/2893972
[6]: http://tonylandis.com/php/php-text-tables-class/
コントローラーでrequire_onceし、ビューファイル内に書いてみました。
“`php
data);
$renderer->showHeaders(true);
?>
= $renderer->render(); ?>
“`
* 全角文字があると罫線がずれる
* 全角と半角が混じると文字化けする
## ArrayToTextTable Fix
array-to-texttable.php の該当箇所を調べながら修正し、望んだような形式で出力できました。
* strlen でバイト数を数えているが、日本語全角は2-3byte。
【変更】SJISに変換し、全角を2文字と数える
* $maxWidth を越えた文字をsubstrで丸めているため文字化ける
【変更】substr ではなく、mb_strcut を使う
> * [PHP: Array to Text Table Generation Class Japanese Multibyte][7]
[7]: https://gist.github.com/DriftwoodJP/6078532
>
取り急ぎ対応したけれど、何かあれば見直そう。
> * [PHP: str_pad – Manual][8]
> * [PHP: strlen – Manual][9]
> * [PHP: mb_strlen – Manual][10]
> * [PHP: mb_convert_encoding – Manual][11]
> * [PHP: substr – Manual][12]
> * [PHP: mb_substr – Manual][13]
> * [PHP: mb_strcut – Manual][14]
[8]: http://php.net/manual/ja/function.str-pad.php
[9]: http://www.php.net/manual/ja/function.strlen.php
[10]: http://php.net/manual/ja/function.mb-strlen.php
[11]: http://php.net/manual/ja/function.mb-convert-encoding.php
[12]: http://php.net/manual/ja/function.substr.php
[13]: http://php.net/manual/ja/function.mb-substr.php
[14]: http://php.net/manual/ja/function.mb-strcut.php
## 補遺
> * [文字コード地獄秘話 第1話:Unicodeにおける全角・半角 – ALBERT Engineering Blog](http://tech.albert2005.co.jp/blog/2014/04/21/mco-eaw/)
[/markdown]