0
0

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 1 year has passed since last update.

【Laravel】正規表現で文字コードが判定されない場合の対処法 + ひらがなを含まない全角英文字のみの正規表現

Last updated at Posted at 2023-05-17

環境
・Windows 10 Home (Bld. 19045.2965)
・VSCode 1.78.2
・Laravel Framework 10.7.1
・PHP 8.1.18

Laravelでpreg_matchを使用し、全角英文字と全角英子文字のみをチェックしようと思った際に少しハマったのでメモ。

全角英文字の正規表現で、

$pattern = "/[A-Z]/";

と書くとひらがなが反応してしまったり

$pattern = "/[\uFF21-\uFF3A]/";

としても何も反応しなかった。
色々調べた結果、

$pattern = "/[\x{FF21}-\x{FF3A}]/u";

で全角英文字のみチェック出来るようになった。

最終的に全角英小文字も追加して

$pattern = "/[\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}]/u";

とした。

参考にした文字コード表

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?