34
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

全角かどうか、半角カナかどうかチェック

Posted at

全角かどうか

preg_match('/^[^ -~。-゚\x00-\x1f\t]+$/u', $value)

[^...]の部分で半角文字以外を表現

正規表現 説明
\t タブ文字
\x00-\x1f 制御文字。念のため
-~。-゚ 半角文字

半角カナかどうか

半角の句読点(。と、)はNG
半角空白OKとする

preg_match('/^[ヲ-゚ー ]+$/u', $value);

ヲ-゚。-゚とすると句読点もOKになる
PHPで半角カタカナの入力チェック(正規表現)

正規表現チェック前にtrimするとき

trim(mb_convert_kana($value, "s", 'UTF-8'));

より確実にトリム処理したいときはこちらのコメントを参照
PHPで全角スペースのtrimがうまくできない時に試すこと

34
43
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?