Share Pastebin
Guest
Public paste!

Toby Inkster

By: a guest | Jan 29th, 2009 | Syntax: PHP | Size: 1.71 KB | Hits: 336 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.  
  3. function ordUTF8 ($c, $index = 0, &$bytes = null)
  4. {
  5.         $len = strlen($c);
  6.         $bytes = 0;
  7.  
  8.         if ($index >= $len)
  9.                 return false;
  10.  
  11.         $h = ord($c{$index});
  12.  
  13.         if ($h <= 0x7F)
  14.         {
  15.                 $bytes = 1;
  16.                 return $h;
  17.         }
  18.         else if ($h < 0xC2)
  19.                 return false;
  20.         else if ($h <= 0xDF && $index < $len - 1)
  21.         {
  22.                 $bytes = 2;
  23.                 return ($h & 0x1F) <<  6 | (ord($c{$index + 1}) & 0x3F);
  24.         }
  25.         else if ($h <= 0xEF && $index < $len - 2)
  26.         {
  27.                 $bytes = 3;
  28.                 return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
  29.                                          | (ord($c{$index + 2}) & 0x3F);
  30.         }          
  31.         else if ($h <= 0xF4 && $index < $len - 3) {
  32.                 $bytes = 4;
  33.                 return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
  34.                                          | (ord($c{$index + 2}) & 0x3F) << 6
  35.                                          | (ord($c{$index + 3}) & 0x3F);
  36.         }
  37.         else
  38.                 return false;
  39. }
  40.  
  41. function xml_entities ($utf8_string, $style='#')
  42. {
  43.         $return = '';
  44.        
  45.         while (strlen($utf8_string))
  46.         {
  47.                 $char        = mb_substr($utf8_string, 0, 1, 'UTF-8');
  48.                 $ord         = ordUTF8($char, 0, $bytes);
  49.                 $utf8_string = substr($utf8_string, $bytes);
  50.                
  51.                 if ($ord == 34)
  52.                         $return .= '&quot;';
  53.                 elseif ($ord == 38)
  54.                         $return .= '&amp;';
  55.                 elseif ($ord == 60)
  56.                         $return .= '&lt;';
  57.                 elseif ($ord == 62)
  58.                         $return .= '&gt;';
  59.                 elseif ($ord == 9 || $ord == 10 || $ord == 13)
  60.                         $return .= $char;
  61.                 elseif ($style == '#' && ($ord < 32 || $ord > 127 || $ord == 39))
  62.                         $return .= '&#'.$ord.';';
  63.                 elseif ($style == '#x' && ($ord < 32 || $ord > 127 || $ord == 39))
  64.                         $return .= '&#'.dechex($ord).';';
  65.                 else
  66.                         $return .= $char;
  67.         }
  68.        
  69.         return $return;
  70. }
  71.  
  72. # Testing...
  73. print xml_entities("Pound is £ & euro is &#8364; & plus or minus is ±\n");
  74.  
  75. ?>