Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. function validate_utf8_string($input, $allow_mb4=true, $return=true)
  2. {
  3.     // Valid UTF-8 sequence?
  4.     if(!preg_match('##u', $input))
  5.     {
  6.         $string = '';
  7.         $len = strlen($input);
  8.         for($i = 0; $i < $len; $i++)
  9.         {
  10.             $c = ord($input[$i]);
  11.             if($c > 128)
  12.             {
  13.                 if($c > 247 || $c <= 191)
  14.                 {
  15.                     if($return)
  16.                     {
  17.                         $string .= '?';
  18.                         continue;
  19.                     }
  20.                     else
  21.                     {
  22.                         return false;
  23.                     }
  24.                 }
  25.                 elseif($c > 239)
  26.                 {
  27.                     $bytes = 4;
  28.                 }
  29.                 elseif($c > 223)
  30.                 {
  31.                     $bytes = 3;
  32.                 }
  33.                 elseif($c > 191)
  34.                 {
  35.                     $bytes = 2;
  36.                 }
  37.                 if(($i + $bytes) > $len)
  38.                 {
  39.                     if($return)
  40.                     {
  41.                         $string .= '?';
  42.                         break;
  43.                     }
  44.                     else
  45.                     {
  46.                         return false;
  47.                     }
  48.                 }
  49.                 $valid = true;
  50.                 $multibytes = $input[$i];
  51.                 while($bytes > 1)
  52.                 {
  53.                     $i++;
  54.                     $b = ord($input[$i]);
  55.                     if($b < 128 || $b > 191)
  56.                     {
  57.                         if($return)
  58.                         {
  59.                             $valid = false;
  60.                             $string .= '?';
  61.                             break;
  62.                         }
  63.                         else
  64.                         {
  65.                             return false;
  66.                         }
  67.                     }
  68.                     else
  69.                     {
  70.                         $multibytes .= $input[$i];
  71.                     }
  72.                     $bytes--;
  73.                 }
  74.                 if($valid)
  75.                 {
  76.                     $string .= $multibytes;
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 $string .= $input[$i];
  82.             }
  83.         }
  84.         $input = $string;
  85.     }
  86.     if($return)
  87.     {
  88.         if($allow_mb4)
  89.         {
  90.             return $input;
  91.         }
  92.         else
  93.         {
  94.             return preg_replace("#[^\\x00-\\x7F][\\x80-\\xBF]{3,}#", '?', $input);
  95.         }
  96.     }
  97.     else
  98.     {
  99.         if($allow_mb4)
  100.         {
  101.             return true;
  102.         }
  103.         else
  104.         {
  105.             return !preg_match("#[^\\x00-\\x7F][\\x80-\\xBF]{3,}#", $input);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement