Skip to content

Instantly share code, notes, and snippets.

@msng
Created March 23, 2012 05:34
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 msng/2167287 to your computer and use it in GitHub Desktop.
Save msng/2167287 to your computer and use it in GitHub Desktop.
文字列が半角のみでできているかどうかチェックするのもうこれでいいんじゃないの
<?php
function is_hankaku($str, $include_kana = false, $include_controls = false, $encoding = null) {
if (!$include_controls && !ctype_print($str)) {
return false;
}
if (is_null($encoding)) {
$encoding = mb_internal_encoding();
}
if ($include_kana) {
$to_encoding = 'SJIS';
} else {
$to_encoding = 'UTF-8';
}
$str = mb_convert_encoding($str, $to_encoding, $encoding);
if (strlen($str) === mb_strlen($str, $to_encoding)) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment