note 75952 added to function.utf8-decode

From: Date: Sat, 23 Jun 2007 16:08:01 +0000
Subject: note 75952 added to function.utf8-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-127901@lists.php.net to get a copy of this message
Following code helped me with mixed (UTF8+ISO-8859-1(x)) encodings. In this case, I have template files made and maintained by designers who do not care about encoding and MySQL data in utf8_binary_ci encoded tables. <?php class Helper { function strSplit($text, $split = 1) { if (!is_string($text)) return false; if (!is_numeric($split) && $split < 1) return false; $len = strlen($text); $array = array(); $i = 0; while ($i < $len) { $key = NULL; for ($j = 0; $j < $split; $j += 1) { $key .= $text{$i}; $i += 1; } $array[] = $key; } return $array; } function UTF8ToHTML($str) { $search = array(); $search[] = "/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e"; $search[] = "/&#228;/"; $search[] = "/&#246;/"; $search[] = "/&#252;/"; $search[] = "/&#196;/"; $search[] = "/&#214;/"; $search[] = "/&#220;/"; $search[] = "/&#223;/"; $replace = array(); $replace[] = 'Helper::_UTF8ToHTML("\\1")'; $replace[] = "ä"; $replace[] = "ö"; $replace[] = "ü"; $replace[] = "Ä"; $replace[] = "Ö"; $replace[] = "ü"; $replace[] = "ß"; $str = preg_replace($search, $replace, $str); return $str; } function _UTF8ToHTML($str) { $ret = 0; foreach((Helper::strSplit(strrev(chr((ord($str{0}) % 252 % 248 % 240 % 224 % 192) + 128).substr($str, 1)))) as $k => $v) $ret += (ord($v) % 128) * pow(64, $k); return "&#".$ret.";"; } } // Usage example: $tpl = file_get_contents("template.tpl"); /* ... */ $row = mysql_fetch_assoc($result); print(Helper::UTF8ToHTML(str_replace("{VAR}", $row['var'], $tpl))); ?> ---- Server IP: 217.160.72.57 Probable Submitter: 84.62.148.58 ---- Manual Page -- http://www.php.net/manual/en/function.utf8-decode.php Edit -- https://master.php.net/note/edit/75952 Del: integrated -- https://master.php.net/note/delete/75952/integrated Del: useless -- https://master.php.net/note/delete/75952/useless Del: bad code -- https://master.php.net/note/delete/75952/bad+code Del: spam -- https://master.php.net/note/delete/75952/spam Del: non-english -- https://master.php.net/note/delete/75952/non-english Del: in docs -- https://master.php.net/note/delete/75952/in+docs Del: other reasons-- https://master.php.net/note/delete/75952 Reject -- https://master.php.net/note/reject/75952 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127901) next »