Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
39
Help us understand the problem. What is going on with this article?
@ka_to

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

More than 5 years have passed since last update.

全角かどうか

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がうまくできない時に試すこと

39
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ka_to

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
39
Help us understand the problem. What is going on with this article?