<?php

/*
Convert UTF-8 encoded files to use numerical character references
i.e. &#20418;
Taken from http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21015725.html
Thanks to Squinky for converting the uni2cnr.txt code to php
I did not test this code !!!
*/

function uni2ncr($content) {
     $chars = array();
     $outline = '';
     preg_match_all('/([\x01-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf])/', $content, $chars);
//     var_dump($chars[0]);
     foreach ($chars[0] as $char) {
           $unival = '';
           switch (strlen($char)) {
           case 1:
               $unival = ord($char) & 0x7f;;
               break;
           case 2:
               $c1 = ord(substr($char, 0, 1)) & 0x1f;
               $c2 = ord(substr($char, 1, 1)) & 0x3f;
               $unival = ($c1 * 0x40) + $c2;
               break;
           case 3:     
               $c1 = ord(substr($char, 0, 1)) & 0x0f;
               $c2 = ord(substr($char, 1, 1)) & 0x3f;
               $c3 = ord(substr($char, 2, 1)) & 0x3f;
               $unival = ($c1 * 0x1000) + ($c2 * 0x40) + $c3;
               break;
           }
           $outline .= "&#$unival;";
      }
      return $outline;
 }

?>
