From 70d107868e677998aa52861b741bb248e8fe6e07 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 7 Sep 2016 13:09:35 +0200 Subject: [PATCH] encoding strlen --- etc/dbs.yaml | 1 + lib/php/db.php | 9 +++++++++ lib/php/db/types/mysql.php | 1 + lib/php/out.php | 15 ++++++++++----- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/etc/dbs.yaml b/etc/dbs.yaml index c0e1c98b..77524aee 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -24,6 +24,7 @@ izi: type: mysql _import: - _izi + encoding: utf-8 tables: site: replace: diff --git a/lib/php/db.php b/lib/php/db.php index 7ca9a620..93c8eb66 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -58,8 +58,17 @@ class Db extends nb { unset($opt['tables']); } + # Encoding + if (isset($opt['encoding'])) { + self::$encoding = $opt['encoding']; + unset($opt['encoding']); + } + # Args into this foreach ($opt as $k=>$v) $this->$k = $v; + #parent::__construct($opt); + #if(!is_scalar($opt['type'])) debug($opt['type']); + if (self::$encoding) out::$charset=self::$encoding; # id if (!empty($this->conf)) { diff --git a/lib/php/db/types/mysql.php b/lib/php/db/types/mysql.php index aed29428..3e31be82 100644 --- a/lib/php/db/types/mysql.php +++ b/lib/php/db/types/mysql.php @@ -11,6 +11,7 @@ $DB_TYPES['mysql'] = array ( 'exec' => array_merge( [ 'SET NAMES '.str_replace('utf-8','utf8',strtolower(Db::$encoding)), + #'SET NAMES '.str_replace(['utf-8','utf8'],['utf8mb4','utf8mb4'],strtolower(Db::$encoding)), ], (Db::p('db.type') ? [ "/*!40103 SET TIME_ZONE='+00:00' */", diff --git a/lib/php/out.php b/lib/php/out.php index 2e146200..2c9d8fa6 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -170,11 +170,11 @@ Class Out extends Nb { } else if (self::is_hash($data[0])) { - $o['head_max_len'] = max(self::ar_map('strlen($a)',array_keys($data[0]))); + $o['head_max_len'] = max(self::ar_map('mb_strlen($a)',array_keys($data[0]))); } elseif (is_array($data[0])) { #debug($data); - $o['head_max_len'] = max(self::ar_map('strlen($a)',$data[0])); + $o['head_max_len'] = max(self::ar_map('mb_strlen($a)',$data[0])); } @@ -223,7 +223,7 @@ Class Out extends Nb { } # NB 10.04.16 if (!self::is_hash($row)) unset($o['head_max_len']); -# NB 10.04.16 if (is_array($row) and count($row)>1) $o['head_max_len'] = strlen(count($row)); +# NB 10.04.16 if (is_array($row) and count($row)>1) $o['head_max_len'] = mb_strlen(count($row)); #if (!self::is_hash($row)) return out_yaml($row,$o); # text @@ -231,7 +231,7 @@ Class Out extends Nb { echo sprintf("%" .(!empty($o['head_max_len']) ? '-'.($o['head_max_len']+1) : '') ."s: %s\n",$k,preg_replace('/\n(\w)/m' - ,"\n".str_repeat(' ',(empty($o['head_max_len']) ? strlen($k) : $o['head_max_len']+1)+2).'\1' + ,"\n".str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k) : $o['head_max_len']+1)+2).'\1' ,self::format($v)) ); } @@ -505,7 +505,12 @@ function out_human(&$row,&$o) { $line = out_csv($row,$o); $i = 0; foreach (explode($o['sep'],$line) as $v) { - $len = strlen($v); +# NB 07.09.16 if (out::$charset == 'utf-8') { +# NB 07.09.16 $len = strlen(utf8_decode($v)); +# NB 07.09.16 } else { +# NB 07.09.16 $len = mb_strlen($v,out::$charset); +# NB 07.09.16 } + $len = mb_strlen($v,out::$charset); if (0 or empty($GLOBALS['_human_len'][$i]) or $len > $GLOBALS['_human_len'][$i] -- 2.47.3