PHP_PHP チュートリアルでの GBK および UTF8 エンコード処理

WBOY
リリース: 2016-07-13 17:38:08
オリジナル
772 人が閲覧しました

1.コーディング範囲
1. GBK (GB2312/GB18030)
x00-xff GBK 2 バイトエンコード範囲
x20-x7f アスキー
xa1-xff 中国語
x80-xff 中国語

2. UTF-8 (ユニコード)
u4e00-u9fa5 (中国語)
x3130-x318F (韓国語)
xAC00-xD7A3 (韓国語)
u0800-u4e00 (日本語)
ps: 韓国語は[u9fa5]より大きい文字です

通常の例:
preg_replace("/([x80-xff])/","",$str);
preg_replace("/([u4e00-u9fa5])/","",$str);

2. コード例

//コンテンツに中国語が含まれているかを判定 - GBK(PHP)
関数 check_is_chinese($s){
return preg_match(/[x80-xff]./, $s);
}
//文字列の長さを取得する - GBK (PHP)
関数 gb_strlen($str){
$count = 0;
for($i=0; $i $s = substr($str, $i, 1);
If (preg_match("/[x80-xff]/", $s)) ++$i;
++$カウント;
}
$count を返します;
}
//インターセプト文字列 string-GBK (PHP)
関数 gb_substr($str, $len){
$count = 0;
for($i=0; $i If($count == $len) ブレーク;
If(preg_match("/[x80-xff]/", substr($str, $i, 1))) ++$i;
++$count;
}
substr($str, 0, $i);を返します
}
//統計文字列の長さ - UTF8 (PHP)
関数 utf8_strlen($str) {
$count = 0;
for($i = 0; $i $value = ord($str[$i]);
If($value > 127) {
$count++;
If($value >= 192 && $value elseif($value >= 224 && $value elseif($value >= 240 && $value else die(UTF-8 互換文字列ではありません);
}
$count++;
}
$count を返します;
}

//文字列をインターセプト-UTF8(PHP)
関数 utf8_substr($str,$position,$length){
$start_position = strlen($str);
$start_byte = 0;
$end_position = strlen($str);
$count = 0;
for($i = 0; $i If($count >= $position && $start_position > $i){
$start_position = $i;
$start_byte = $count;
}
If(($count-$start_byte)>=$length) {
$end_position = $i;
休憩;
                                                                 $value = ord($str[$i]);
If($value > 127){
$count++;
If($value >= 192 && $value elseif($value >= 224 && $value elseif($value >= 240 && $value else die(UTF-8 互換文字列ではありません);
}
$count++;
}
Return(substr($str,$start_position,$end_position-$start_position));
}

//文字列長統計 - UTF8 [中国語の場合は 3 バイト、ロシア語と韓国語の場合は 2 バイト、文字の場合は 1 バイト] (Ruby)
def utf8_string_length(str)
Temp = CGI::unescape(str)
i = 0;
j = 0;
temp.length.times{|t|
気温[t] の場合 i += 1
elseif temp[t] >= 127 および temp[t] j += 1
if 0 == (j % 2)
私 += 2
j = 0
終了
その他
j += 1
if 0 == (j % 3)
i +=2
j = 0
終了
終了
}
返してください
}

//韓国語UTF-8(JavaScript)が含まれているかどうかを判定します
関数 checkKoreaChar(str) {
for(i=0; i if(((str.charCodeAt(i) > 0x3130 && str.charCodeAt(i) < 0x318F) || (str.charCodeAt(i) >= 0xAC00 && str.charCodeAt(i) <= 0xD7A3)) ){
true を返します;
}
}
false を返します;
}

//漢字の有無を判定 - GBK (javascript)
関数 check_chinese_char(s){
Return (s.length != s.replace(/[^x00-xff]/g,"**").length);
}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/486508.html技術記事 1. エンコード範囲 1. GBK (GB2312/GB18030) x00-xff GBK 2 バイトエンコード範囲 x20-x7f ASCII xa1-xff 中国語 x80-xff 中国語 2. UTF-8 (Unicode) u4e00-u9fa5 (中国語) x3130-x318F (韓国語)...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!