Last active
December 14, 2020 04:41
-
-
Save Jeemusu/7525186 to your computer and use it in GitHub Desktop.
Check if string is full-width Japanese. Useful when you need to validate a name field in Japanese.
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
function jpn_zenkaku_only($str) { | |
$encoding = "UTF-8"; | |
// Get length of string | |
$len = mb_strlen($str, $encoding); | |
// Check each character | |
for ($i = 0; $i < $len; $i++) { | |
$char = mb_substr($str, $i, 1, $encoding); | |
// Check for non-printable characters | |
if (ctype_print($char)) { | |
return false; | |
} | |
// Convert to SHIFT-JIS to include kana characters | |
$char = mb_convert_encoding($char, 'SJIS', $encoding); | |
// Check if string lengths match | |
if (strlen($char) === mb_strlen($char, 'SJIS')) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment