note 46410 added to function.utf8-decode

From: Date: Sun, 10 Oct 2004 06:58:29 +0000
Subject: note 46410 added to function.utf8-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-78262@lists.php.net to get a copy of this message
I'm making a web in Vietnamese. My DB stores data in UTF-8 format. But I need to convert it no NCR format and I wrote a function for this problem. // Convert a string from UTF-8 format to NCR format for all language // Use first character and up to 5-next characters // Written by Cuong V. Nguyen, mrcuongnv@vnu.edu.vn // K46CA, College of Applied Scientific and Technology // Vietnam National University, Hanoi function utf8_to_ncr($utf8) { $len = strlen($utf8); $ncr = ""; $i = 0; while ($i < $len) { $code = 0; $unit = ord($utf8[$i]); if ($unit < 0x80) { $code = $unit; $next = 0; } elseif ($unit < 0xE0) { $code = $unit & 0x1F; $next = 1; } elseif ($unit < 0xF0) { $code = $unit & 0x0F; $next = 2; } elseif ($unit < 0xF8) { $code = $unit & 0x07; $next = 3; } elseif ($unit < 0xFC) { $code = $unit & 0x03; $next = 4; } elseif ($unit < 0xFE) { $code = $unit & 0x01; $next = 5; } for ($j = 0; $j < $next; $j++) { $i++; $unit = ord($utf8[$i]); $code = ($code << 6) | ($unit & 0x3F); } if ($code < 0x80) { $ncr .= chr($code); } else { $ncr .= "&#" . $code . ";"; } $i++; } return $ncr; } ---- Manual Page -- http://www.php.net/manual/en/function.utf8-decode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46410 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46410&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46410&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78262) next »