Created
March 23, 2012 05:34
-
-
Save msng/2167287 to your computer and use it in GitHub Desktop.
文字列が半角のみでできているかどうかチェックするのもうこれでいいんじゃないの
This file contains hidden or 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 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