2007年7月25日 星期三

PHP中檢查字串是否含有非中文字

來源:
function chinese_only($arg_strContent)
{
$blnChineseOnly= true;
$intLoopCount = 0;
$intContentLength = strlen($arg_strContent);
while ($intLoopCount < $intContentLength) { $chrSingle = substr($arg_strContent,$intLoopCount,1); if(ord($chrSingle) > 0x80)
{
$chrSingle = substr($arg_strContent,$intLoopCount,2);
$intLoopCount++;
if(!isBig5($chrSingle))
{
$blnChineseOnly = false;
break;
}
}
else
{
$blnChineseOnly = false;
break;
}
$intLoopCount++;

}
return !$blnChineseOnly;
}
function isBig5($char="")
{
$bc = hexdec(bin2hex($char));
if(($bc>=0xa440 &&amp;amp; $bc<= 0xc67e)||($bc>=0xc940 && $bc<= 0xf9dc)) { return true; } else { return false; } } $aa='中文字'; echo '$aa='.$aa.',變數中是否含有非中文字 = '.chinese_only($aa)."
";
$aa='abcㄅㄆㄇ';
echo '$aa='.$aa.',變數中是否含有非中文字 = '.chinese_only($aa)."
";
$aa='中文ㄅㄆㄇ';
echo '$aa='.$aa.',變數中是否含有非中文字 = '.chinese_only($aa)."
";

【下列文章您可能也有興趣】

沒有留言: