2
1

More than 1 year has passed since last update.

正規表現(PHP)

Last updated at Posted at 2022-12-22

正規表現のサンプルプログラムです。
今回は拡張子が「.jpg」または「.png」だったら「YES」、それ以外だったら「NO」をブラウザに出力する仕様です。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>正規表現テスト</title>
</head>
<body>
  <?php
      $str = "test1.XLS";
      
      if ((preg_match("/\.jpe?g\z/i",$str) == 1) || (preg_match("/\.png\z/i",$str) == 1)){
        echo "YES";
      } else {
        echo "NO";
      }

      
  ?>
</body>
</html>

PHPでは

<?php
      $str = "test1.XLS";
      
      if ((preg_match("/\.jpe?g\z/i",$str) == 1) || (preg_match("/\.png\z/i",$str) == 1)){
        echo "YES";
      } else {
        echo "NO";
      }

      
  ?>

//「.jpg」または「.png」の時に「YES」
 if ((preg_match("/\.jpe?g\z/i",$str) == 1) || (preg_match("/\.png\z/i",$str) == 1)){
        echo "YES";
      } else {
        echo "NO";
      }

のようにコーディングします。

2
1
2

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
2
1