php判斷是否有韓文(上海地鐵包被車門夾住)

  • 1、php有沒有檢測輸入的字元串是什麼語言種類的方法
  • 2、用PHP正則判斷 只能輸入 中文、韓文、日文、英文(大小寫) 除外的一律禁止的。 有誰可以么?
  • 3、用PHP正則判斷 只能輸入 中文、韓文、日文、因為(大小寫) 除外的一律禁止的。 有誰可以么?
  • 4、php 中文和編碼判斷代碼
  • 5、php中如何判斷中英文字元

那個你可以粗略根據編碼格式來:

?php

$lang = substr($_SERVER[‘HTTP_ACCEPT_LANGUAGE’], 0, 4); //只取前4位,這樣只判斷最優先的語言。如果取前5位,可能出現en,zh的情況,影響判斷。

if (preg_match(“/zh-c/i”, $lang))

echo “簡體中文”;

else if (preg_match(“/zh/i”, $lang))

echo “繁體中文”;

else if (preg_match(“/en/i”, $lang))

echo “English”;

else if (preg_match(“/fr/i”, $lang))

echo “French”;

else if (preg_match(“/de/i”, $lang))

echo “German”;

else if (preg_match(“/jp/i”, $lang))

echo “Japanese”;

else if (preg_match(“/ko/i”, $lang))

echo “Korean”;

else if (preg_match(“/es/i”, $lang))

echo “Spanish”;

else if (preg_match(“/sv/i”, $lang))

echo “Swedish”;

else echo $_SERVER[“HTTP_ACCEPT_LANGUAGE”];

?

可以用兩個表達式來實現

首先匹配到特殊字元 如 ~!@#$%^*()則返回失敗,

如果匹配不到特殊字元 然後再排除數字 用 ‘/([^\d]+)/’匹配

如果再成功 才算成功 不然你說的那麼多種字元UTF-8編碼會很難寫 而且效率也很差

/^[\x{4e00}-\x{9fa5}\x{3130}-\x{318F}\x{0800}-\x{4e00}a-zA-Z]+$/u

別忘了最後那個修正符「 u 」,少了它可不行。

再補充下,要使用這個正則,你的網頁編碼必須是UTF-8,如果是GB2312,那不要想了。

編碼範圍1.

GBK

(GB2312/GB18030)

\x00-\xff

GBK雙位元組編碼範圍

\x20-\x7f

ASCII

\xa1-\xff

中文

\x80-\xff

中文

2.

UTF-8

(Unicode)

\u4e00-\u9fa5

(中文)

\x3130-\x318F

(韓文

\xAC00-\xD7A3

(韓文)

\u0800-\u4e00

(日文)

ps:

韓文是大於[\u9fa5]的字元

正則例子:

preg_replace(」/([\x80-\xff])/」,」”,$str);

preg_replace(」/([u4e00-u9fa5])/」,」”,$str);

二、代碼例子

複製代碼

代碼如下:

//判斷內容里有沒有中文-GBK

(PHP)

function

check_is_chinese($s){

return

preg_match(‘/[\x80-\xff]./’,

$s);

}

//獲取字元串長度-GBK

(PHP)

function

gb_strlen($str){

$count

=

0;

for($i=0;

$istrlen($str);

$i++){

$s

=

substr($str,

$i,

1);

if

(preg_match(“/[\x80-\xff]/”,

$s))

++$i;

++$count;

}

return

$count;

}

//截取字元串字串-GBK

(PHP)

function

gb_substr($str,

$len){

$count

=

0;

for($i=0;

$istrlen($str);

$i++){

if($count

==

$len)

break;

if(preg_match(“/[\x80-\xff]/”,

substr($str,

$i,

1)))

++$i;

++$count;

}

return

substr($str,

0,

$i);

}

//統計字元串長度-UTF8

(PHP)

function

utf8_strlen($str)

{

$count

=

0;

for($i

=

0;

$i

strlen($str);

$i++){

$value

=

ord($str[$i]);

if($value

127)

{

$count++;

if($value

=

192

$value

=

223)

$i++;

elseif($value

=

224

$value

=

239)

$i

=

$i

+

2;

elseif($value

=

240

$value

=

247)

$i

=

$i

+

3;

else

die(‘Not

a

UTF-8

compatible

string’);

}

$count++;

}

return

$count;

}

//截取字元串-UTF8(PHP)

function

utf8_substr($str,$position,$length){

$start_position

=

strlen($str);

$start_byte

=

0;

$end_position

=

strlen($str);

$count

=

0;

for($i

=

0;

$i

strlen($str);

$i++){

if($count

=

$position

$start_position

$i){

$start_position

=

$i;

$start_byte

=

$count;

}

if(($count-$start_byte)=$length)

{

$end_position

=

$i;

break;

}

$value

=

ord($str[$i]);

if($value

127){

$count++;

if($value

=

192

$value

=

223)

$i++;

elseif($value

=

224

$value

=

239)

$i

=

$i

+

2;

elseif($value

=

240

$value

=

247)

$i

=

$i

+

3;

else

die(‘Not

a

UTF-8

compatible

string’);

}

$count++;

}

return(substr($str,$start_position,$end_position-$start_position));

}

//判斷是否是有韓文-UTF-8

(JavaScript)

function

checkKoreaChar(str)

{

for(i=0;

istr.length;

i++)

{

if(((str.charCodeAt(i)

0x3130

str.charCodeAt(i)

0x318F)

||

(str.charCodeAt(i)

=

0xAC00

str.charCodeAt(i)

=

0xD7A3)))

{

return

true;

}

}

return

false;

}

//判斷是否有中文字元-GBK

(JavaScript)

function

check_chinese_char(s){

return

(s.length

!=

s.replace(/[^\x00-\xff]/g,”**”).length);

}

PHP判斷中英文的依據是字元的ASII值,而字元的ASII值也因編碼不同而不同。為了能編寫判斷中英文字元的php程序,我們必須先來了解下各編碼下中文英文字元的ASII值範圍:

1.

GBK

(GB2312/GB18030)

x00-xff

GBK雙位元組編碼範圍

x20-x7f

ASCII

xa1-xff

中文

gb2312

x80-xff

中文

gbk

2.

UTF-8

(Unicode)

u4e00-u9fa5

(中文)

x3130-x318F

(韓文

xAC00-xD7A3

(韓文)

u0800-u4e00

(日文)

!DOCTYPE HTML PUBLIC

“-//W3C//DTD

HTML

4.0

Transitional//EN”

HTML

HEAD

TITLE

New

Document

/TITLE

META

http-equiv=”Content-Type”

content=”text/html;

charset=utf-8″

/HEAD

BODY

?

$str

=

“中文”;

echo

$str;

echo

“hr”;

//if

(preg_match(“/^[“.chr(0xa1).”-“.chr(0xff).”]+$/”,

$str))

{

//只能在GB2312情況下使用

if

(preg_match(“/^[x7f-xff]+$/”,

$str))

{

//兼容gb2312,utf-8

echo

“正確輸入”;

}

else

{

echo

“錯誤輸入”;

}

?

/BODY

/HTML

原創文章,作者:RR8MO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126481.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
RR8MO的頭像RR8MO
上一篇 2024-10-03 23:08
下一篇 2024-10-03 23:08

相關推薦

發表回復

登錄後才能評論