cvs: pear /Console_Table Table.php

From: Date: Wed, 22 Feb 2006 22:23:34 +0000
Subject: cvs: pear /Console_Table Table.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-38512@lists.php.net to get a copy of this message
yunosh Wed Feb 22 22:23:34 2006 UTC Modified files: /pear/Console_Table Table.php Log: Add multibyte support (Request 2934). http://cvs.php.net/viewcvs.cgi/pear/Console_Table/Table.php?r1=1.12&r2=1.13&diff_format=u Index: pear/Console_Table/Table.php diff -u pear/Console_Table/Table.php:1.12 pear/Console_Table/Table.php:1.13 --- pear/Console_Table/Table.php:1.12 Wed Feb 22 22:10:10 2006 +++ pear/Console_Table/Table.php Wed Feb 22 22:23:34 2006 @@ -33,7 +33,7 @@ // | Jan Schneider <jan@horde.org> | // +-----------------------------------------------------------------------+ // -// $Id: Table.php,v 1.12 2006/02/22 22:10:10 yunosh Exp $ +// $Id: Table.php,v 1.13 2006/02/22 22:23:34 yunosh Exp $ // // Utility for printing tables from cmdline scripts // @@ -125,6 +125,13 @@ /** + * Charset of the data + * @var string + */ + var $_charset = 'utf-8'; + + + /** * Constructor * * @param int $align Default alignment @@ -182,6 +189,15 @@ $this->_filters[] = array($col, &$callback); } + /** + * Sets the charset of the provided table data. + * + * @string $charset A charset supported by the mbstring PHP extension. + */ + function setCharset($charset) + { + $this->_charset = $charset; + } /** * Sets the alignment for the columns @@ -407,7 +423,7 @@ for ($i=0; $i<count($rows); $i++) { for ($j=0; $j<count($rows[$i]); $j++) { - if ($rows[$i] != CONSOLE_TABLE_HORIZONTAL_RULE AND strlen($rows[$i][$j]) < $this->_cell_lengths[$j]) { + if ($rows[$i] != CONSOLE_TABLE_HORIZONTAL_RULE AND $this->_strlen($rows[$i][$j]) < $this->_cell_lengths[$j]) { $rows[$i][$j] = str_pad($rows[$i][$j], $this->_cell_lengths[$j], ' ', $this->_col_align[$j]); } } @@ -467,7 +483,7 @@ } for ($i=0; $i<count($this->_headers); $i++) { - if (strlen($this->_headers[$i]) < $this->_cell_lengths[$i]) { + if ($this->_strlen($this->_headers[$i]) < $this->_cell_lengths[$i]) { $this->_headers[$i] = str_pad($this->_headers[$i], $this->_cell_lengths[$i], ' ', $this->_col_align[$i]); } } @@ -521,8 +537,31 @@ function _calculateCellLengths($row) { for ($i=0; $i<count($row); $i++) { - $this->_cell_lengths[$i] = max(strlen(@$this->_headers[$i]), @$this->_cell_lengths[$i], strlen(@$row[$i])); + $this->_cell_lengths[$i] = max($this->_strlen(@$this->_headers[$i]), @$this->_cell_lengths[$i], $this->_strlen(@$row[$i])); + } + } + + /** + * Returns the character length of a string. + * + * @param string $str A multibyte or singlebyte string. + * + * @return integer The string length. + */ + function _strlen($str) + { + static $mbstring; + + // Cache expensive function_exists() calls. + if (!isset($mbstring)) { + $mbstring = function_exists('mb_strlen'); } + + if ($mbstring) { + return mb_strlen($str, $this->_charset); + } + + return strlen($str); } + } -?> \ No newline at end of file

« previous php.pear.cvs (#38512) next »