teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

修正

2017/02/14 07:52

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -30,7 +30,7 @@
30
30
  バリデートするとか、日付形式の文字かどうかをcheckdate()するとか
31
31
  色々やりようはあります
32
32
 
33
- ```PHP
33
+ ```HTML
34
34
  <form method="post"><input type="text" name="a" value="2017-02-14"><input type="submit" value="go"></form>
35
35
  <form method="post"><input type="text" name="a" value="2017/02/15"><input type="submit" value="go"></form>
36
36
  <form method="post"><input type="text" name="a" value="20170216"><input type="submit" value="go"></form>
@@ -43,19 +43,20 @@
43
43
  <form method="post"><input type="text" name="a" value="9999-12-31"><input type="submit" value="go"></form>
44
44
  <form method="post"><input type="text" name="a" value="2017-00-01"><input type="submit" value="go"></form>
45
45
  <form method="post"><input type="text" name="a" value="2017-01-40"><input type="submit" value="go"></form>
46
- </form>
46
+ ```
47
- <?PHP
47
+ ```PHP
48
48
  $pattern='#^(\d{4})([/-]?)(\d{2})\2(\d{2})$#';
49
- $a=filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
49
+ $a=!isset($_POST["a"])?NULL:filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
50
50
  'options'=>[
51
- 'default'=> NULL,
51
+ 'default'=> "",
52
52
  'regexp'=> $pattern
53
53
  ],
54
54
  ]);
55
+ if(!is_null($a)){
55
- if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
56
+ if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
56
- print date("Y-m-d",strtotime($a))."<br>";
57
+ print date("Y-m-d",strtotime($a))."<br>";
57
- }else{
58
+ }else{
58
- print "bad data!";
59
+ print "bad data!";
60
+ }
59
61
  }
60
-
61
62
  ```

3

おまけ

2017/02/14 07:52

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -23,4 +23,39 @@
23
23
  print "ng<br>";
24
24
  }
25
25
  }
26
+ ```
27
+
28
+ # おまけ
29
+ POSTで受けたデータを前提とするのであれば、filter_inputで先に
30
+ バリデートするとか、日付形式の文字かどうかをcheckdate()するとか
31
+ 色々やりようはあります
32
+
33
+ ```PHP
34
+ <form method="post"><input type="text" name="a" value="2017-02-14"><input type="submit" value="go"></form>
35
+ <form method="post"><input type="text" name="a" value="2017/02/15"><input type="submit" value="go"></form>
36
+ <form method="post"><input type="text" name="a" value="20170216"><input type="submit" value="go"></form>
37
+ <form method="post"><input type="text" name="a" value="2017/2/17"><input type="submit" value="go"></form>
38
+ <form method="post"><input type="text" name="a" value="2017020180"><input type="submit" value="go"></form>
39
+ <form method="post"><input type="text" name="a" value="2017/02-19"><input type="submit" value="go"></form>
40
+ <form method="post"><input type="text" name="a" value="xxxx-yy-zz"><input type="submit" value="go"></form>
41
+ <form method="post"><input type="text" name="a" value="0000-00-00"><input type="submit" value="go"></form>
42
+ <form method="post"><input type="text" name="a" value="0001-01-01"><input type="submit" value="go"></form>
43
+ <form method="post"><input type="text" name="a" value="9999-12-31"><input type="submit" value="go"></form>
44
+ <form method="post"><input type="text" name="a" value="2017-00-01"><input type="submit" value="go"></form>
45
+ <form method="post"><input type="text" name="a" value="2017-01-40"><input type="submit" value="go"></form>
46
+ </form>
47
+ <?PHP
48
+ $pattern='#^(\d{4})([/-]?)(\d{2})\2(\d{2})$#';
49
+ $a=filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
50
+ 'options'=>[
51
+ 'default'=> NULL,
52
+ 'regexp'=> $pattern
53
+ ],
54
+ ]);
55
+ if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
56
+ print date("Y-m-d",strtotime($a))."<br>";
57
+ }else{
58
+ print "bad data!";
59
+ }
60
+
26
61
  ```

2

修正

2017/02/14 07:46

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -14,7 +14,7 @@
14
14
  ];
15
15
  /* $pattern="#^\d{4}[/-]{0,1}\d{2}[/-]{0,1}\d{2}$#"; */
16
16
  /* おかしなパターンを拾うのでいかに修正 */
17
- $pattern="#^\d{4}([/-]{0,1})\d{2}\\1\d{2}$#";
17
+ $pattern="#^\d{4}([/-]?)\d{2}\\1\d{2}$#";
18
18
  foreach ($a as $val){
19
19
  print $val." is ";
20
20
  if(preg_match($pattern,$val,$match)){

1

パターン修正

2017/02/14 04:25

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -9,10 +9,12 @@
9
9
 
10
10
  ```PHP
11
11
  $a=["2017-02-14","2017/02/15","20170216",//問題なくマッチ
12
- "2017/2/17","2017020180","xxxx-yy-zz",//エラー
12
+ "2017/2/17","2017020180","2017/02-19","xxxx-yy-zz",//エラー
13
13
  "0000-00-00","9999-99-99" //極端な数字でもマッチ
14
14
  ];
15
- $pattern="#^\d{4}[/-]{0,1}\d{2}[/-]{0,1}\d{2}$#";
15
+ /* $pattern="#^\d{4}[/-]{0,1}\d{2}[/-]{0,1}\d{2}$#"; */
16
+ /* おかしなパターンを拾うのでいかに修正 */
17
+ $pattern="#^\d{4}([/-]{0,1})\d{2}\\1\d{2}$#";
16
18
  foreach ($a as $val){
17
19
  print $val." is ";
18
20
  if(preg_match($pattern,$val,$match)){