Skip to content

Instantly share code, notes, and snippets.

@mjj2000
Last active September 5, 2018 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjj2000/b2b70ddf75d2209124554e0646c88733 to your computer and use it in GitHub Desktop.
Save mjj2000/b2b70ddf75d2209124554e0646c88733 to your computer and use it in GitHub Desktop.
Check if string has any big5 character in PHP
<?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