Last active
September 5, 2018 05:09
-
-
Save mjj2000/b2b70ddf75d2209124554e0646c88733 to your computer and use it in GitHub Desktop.
Check if string has any big5 character in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isBig5Char ($char) { | |
if (strlen($char) < 2) | |
return false; | |
$byte1hex = dechex(ord($char[0])); | |
$byte2hex = dechex(ord($char[1])); | |
$number = hexdec($byte1hex . $byte2hex); | |
return ( | |
0x8140 <= $number && | |
$number <= 0xFEFE | |
); | |
} | |
function hasBig5 ($str) { | |
if (strlen($str) < 2) | |
return false; | |
for ($i = 0; $i < (strlen($str) - 1); $i++) { | |
if (isBig5Char(substr($str, $i, 2))) { | |
return true; | |
} | |
} | |
return false; | |
} | |
$text = '寶'; | |
$big5_text = iconv("UTF-8", "BIG5//IGNORE", $text); | |
if (hasBig5($big5_text)) { | |
echo 'yes'; | |
} else { | |
echo 'no'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment