Skip to content

Instantly share code, notes, and snippets.

@Jeemusu
Last active December 14, 2020 04:41
Show Gist options
  • Save Jeemusu/7525186 to your computer and use it in GitHub Desktop.
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.
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