PHP lib utf8.php

1 개요[ | ]

PHP lib_utf8.php
  • UTF-8 문자열 처리 PHP 함수 모음

2 utf2euc(), euc2utf()[ | ]

function utf2euc($str) { return iconv("UTF-8","cp949//IGNORE", $str); }
function euc2utf($str) { return iconv("cp949","UTF-8//IGNORE", $str); }

3 charAt8()[ | ]

function charAt8($str, $num) { return mb_substr($str, $num, 1, 'UTF-8'); }

4 chr8()[ | ]

function chr8($num) {
   if($num<128) return chr($num);
   if($num<2048) return chr(($num>>6)+192).chr(($num&63)+128);
   if($num<65536) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
   if($num<2097152) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
   return false;
}

5 ord8()[ | ]

function ord8($ch) {
  $len = strlen($ch);
  if($len <= 0) return false;
  $h = ord($ch{0});
  if ($h <= 0x7F) return $h;
  if ($h < 0xC2) return false;
  if ($h <= 0xDF && $len>1) return ($h & 0x1F) <<  6 | (ord($ch{1}) & 0x3F);
  if ($h <= 0xEF && $len>2) return ($h & 0x0F) << 12 | (ord($ch{1}) & 0x3F) << 6 | (ord($ch{2}) & 0x3F);          
  if ($h <= 0xF4 && $len>3) return ($h & 0x0F) << 18 | (ord($ch{1}) & 0x3F) << 12 | (ord($ch{2}) & 0x3F) << 6 | (ord($ch{3}) & 0x3F);
  return false;
}

6 strlen8()[ | ]

function strlen8($str) { return mb_strlen($str, 'UTF-8'); }

7 substr8()[ | ]

function substr8($str, $start, $len=-1) {
  if($len==-1)$len = mb_strlen($str, 'UTF-8')-$start;
  return mb_substr($str, $start, $len, 'UTF-8');
}

8 같이 보기[ | ]

9 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}