回答編集履歴

4

修正

2017/02/14 07:52

投稿

yambejp
yambejp

スコア114863

test CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
 
64
64
 
65
- ```PHP
65
+ ```HTML
66
66
 
67
67
  <form method="post"><input type="text" name="a" value="2017-02-14"><input type="submit" value="go"></form>
68
68
 
@@ -88,17 +88,17 @@
88
88
 
89
89
  <form method="post"><input type="text" name="a" value="2017-01-40"><input type="submit" value="go"></form>
90
90
 
91
- </form>
91
+ ```
92
92
 
93
- <?PHP
93
+ ```PHP
94
94
 
95
95
  $pattern='#^(\d{4})([/-]?)(\d{2})\2(\d{2})$#';
96
96
 
97
- $a=filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
97
+ $a=!isset($_POST["a"])?NULL:filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
98
98
 
99
99
  'options'=>[
100
100
 
101
- 'default'=> NULL,
101
+ 'default'=> "",
102
102
 
103
103
  'regexp'=> $pattern
104
104
 
@@ -106,16 +106,18 @@
106
106
 
107
107
  ]);
108
108
 
109
- if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
109
+ if(!is_null($a)){
110
110
 
111
- print date("Y-m-d",strtotime($a))."<br>";
111
+ if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
112
112
 
113
- }else{
113
+ print date("Y-m-d",strtotime($a))."<br>";
114
114
 
115
+ }else{
116
+
115
- print "bad data!";
117
+ print "bad data!";
118
+
119
+ }
116
120
 
117
121
  }
118
122
 
119
-
120
-
121
123
  ```

3

おまけ

2017/02/14 07:52

投稿

yambejp
yambejp

スコア114863

test CHANGED
@@ -49,3 +49,73 @@
49
49
  }
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ # おまけ
56
+
57
+ POSTで受けたデータを前提とするのであれば、filter_inputで先に
58
+
59
+ バリデートするとか、日付形式の文字かどうかをcheckdate()するとか
60
+
61
+ 色々やりようはあります
62
+
63
+
64
+
65
+ ```PHP
66
+
67
+ <form method="post"><input type="text" name="a" value="2017-02-14"><input type="submit" value="go"></form>
68
+
69
+ <form method="post"><input type="text" name="a" value="2017/02/15"><input type="submit" value="go"></form>
70
+
71
+ <form method="post"><input type="text" name="a" value="20170216"><input type="submit" value="go"></form>
72
+
73
+ <form method="post"><input type="text" name="a" value="2017/2/17"><input type="submit" value="go"></form>
74
+
75
+ <form method="post"><input type="text" name="a" value="2017020180"><input type="submit" value="go"></form>
76
+
77
+ <form method="post"><input type="text" name="a" value="2017/02-19"><input type="submit" value="go"></form>
78
+
79
+ <form method="post"><input type="text" name="a" value="xxxx-yy-zz"><input type="submit" value="go"></form>
80
+
81
+ <form method="post"><input type="text" name="a" value="0000-00-00"><input type="submit" value="go"></form>
82
+
83
+ <form method="post"><input type="text" name="a" value="0001-01-01"><input type="submit" value="go"></form>
84
+
85
+ <form method="post"><input type="text" name="a" value="9999-12-31"><input type="submit" value="go"></form>
86
+
87
+ <form method="post"><input type="text" name="a" value="2017-00-01"><input type="submit" value="go"></form>
88
+
89
+ <form method="post"><input type="text" name="a" value="2017-01-40"><input type="submit" value="go"></form>
90
+
91
+ </form>
92
+
93
+ <?PHP
94
+
95
+ $pattern='#^(\d{4})([/-]?)(\d{2})\2(\d{2})$#';
96
+
97
+ $a=filter_input(INPUT_POST,'a',FILTER_VALIDATE_REGEXP,[
98
+
99
+ 'options'=>[
100
+
101
+ 'default'=> NULL,
102
+
103
+ 'regexp'=> $pattern
104
+
105
+ ],
106
+
107
+ ]);
108
+
109
+ if(preg_match($pattern,$a,$m) and checkdate($m[3],$m[4],$m[1])){
110
+
111
+ print date("Y-m-d",strtotime($a))."<br>";
112
+
113
+ }else{
114
+
115
+ print "bad data!";
116
+
117
+ }
118
+
119
+
120
+
121
+ ```

2

修正

2017/02/14 07:46

投稿

yambejp
yambejp

スコア114863

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  /* おかしなパターンを拾うのでいかに修正 */
32
32
 
33
- $pattern="#^\d{4}([/-]{0,1})\d{2}\\1\d{2}$#";
33
+ $pattern="#^\d{4}([/-]?)\d{2}\\1\d{2}$#";
34
34
 
35
35
  foreach ($a as $val){
36
36
 

1

パターン修正

2017/02/14 04:25

投稿

yambejp
yambejp

スコア114863

test CHANGED
@@ -20,13 +20,17 @@
20
20
 
21
21
  $a=["2017-02-14","2017/02/15","20170216",//問題なくマッチ
22
22
 
23
- "2017/2/17","2017020180","xxxx-yy-zz",//エラー
23
+ "2017/2/17","2017020180","2017/02-19","xxxx-yy-zz",//エラー
24
24
 
25
25
  "0000-00-00","9999-99-99" //極端な数字でもマッチ
26
26
 
27
27
  ];
28
28
 
29
- $pattern="#^\d{4}[/-]{0,1}\d{2}[/-]{0,1}\d{2}$#";
29
+ /* $pattern="#^\d{4}[/-]{0,1}\d{2}[/-]{0,1}\d{2}$#"; */
30
+
31
+ /* おかしなパターンを拾うのでいかに修正 */
32
+
33
+ $pattern="#^\d{4}([/-]{0,1})\d{2}\\1\d{2}$#";
30
34
 
31
35
  foreach ($a as $val){
32
36