Maison >php教程 >php手册 >php对用户输入的非标准格式日期匹配

php对用户输入的非标准格式日期匹配

WBOY
WBOYoriginal
2016-06-06 19:53:211118parcourir

这两天在做的项目中需要对用户输入的日期进行匹配,匹配的格式范围 包括: 2001.4.3 六月五号 七月十五 一一年四月三日 二零一三年五月六日 01年3月7日 2011年3月31日 今天、明天、下周一、周六 使用的是正则来处理,值得注意的是再utf8编码下,正则使用\u选

这两天在做的项目中需要对用户输入的日期进行匹配,匹配的格式范围 包括:

2001.4.3

六月五号

七月十五

一一年四月三日

二零一三年五月六日

01年3月7日

2011年3月31日

今天、明天、下周一、周六

使用的是正则来处理,值得注意的是再utf8编码下,正则使用\u选项就能在表达式中直接输入中文。一下贴出代码:

<span>  1</span>     <span>//</span><span>时间标识%6.5%</span>
<span>  2</span>     <span>private</span> <span>function</span> analyze_time( <span>$content</span><span> ){
</span><span>  3</span>         <span>$time</span> = <span>false</span><span>;
</span><span>  4</span> 
<span>  5</span>         <span>if</span>( <span>preg_match</span>( '/%.*?%/', <span>$content</span>, <span>$matches</span><span> ) ){
</span><span>  6</span>             <span>$time_str</span> = <span>trim</span>( <span>substr</span>( <span>$matches</span>[0], 1, <span>strlen</span>( <span>$matches</span>[0] ) - 2<span> ) );
</span><span>  7</span>             <span>$time</span> = <span>$this</span> -> analyze_time_day( <span>$time_str</span><span> );
</span><span>  8</span>             (!<span>$time</span> ) && <span>$time</span> = <span>$this</span> -> analyze_time_week( <span>$time_str</span><span> );
</span><span>  9</span>             (!<span>$time</span> ) && <span>$time</span> = <span>$this</span> -> analyze_time_date( <span>$time_str</span><span> );
</span><span> 10</span> <span>        }
</span><span> 11</span>         <span>return</span> <span>$time</span><span>;
</span><span> 12</span> <span>    }
</span><span> 13</span> 
<span> 14</span>     <span>function</span> analyze_time_day( <span>$content</span><span> ){
</span><span> 15</span>         <span>$day</span> = <span>array</span><span>(
</span><span> 16</span>             '今天' => 0,
<span> 17</span>             '明天' => 3600 * 24,
<span> 18</span>             '后天' => 3600 * 48,
<span> 19</span>             '大后天' => 3600 * 72,
<span> 20</span> <span>        );
</span><span> 21</span>         <span>return</span> <span>isset</span>( <span>$day</span>[<span>$content</span>] )?<span>$day</span>[<span>$content</span>]:<span>false</span><span>;
</span><span> 22</span> <span>    }
</span><span> 23</span> 
<span> 24</span> <span>function</span> analyze_time_week( <span>$content</span><span> ){
</span><span> 25</span> 
<span> 26</span> 
<span> 27</span>         <span>$now</span> =<span> now();
</span><span> 28</span>         <span>$time</span> = 0<span>;
</span><span> 29</span>         <span>$week</span> = <span>array</span>( "周", "礼拜", "星期"<span> );
</span><span> 30</span>         <span>$week_pre</span> = <span>array</span>( "下", "下个"<span> );
</span><span> 31</span>         <span>$week_day</span> = <span>array</span>( "一" => 1, "二" => 2, "三" => 3, "四" => 4, "五" => 5, "六" => 6, "天" => 7, "日" => 7<span> );
</span><span> 32</span> 
<span> 33</span>         <span>//</span><span>模式一,匹配 "周一" , "下周一", "周日";</span>
<span> 34</span>         <span>$reg1</span> = "/^(" . <span>implode</span>( "|", <span>$week_pre</span> ) . ")?(" . <span>implode</span>( "|", <span>$week</span> ) . ")(" . <span>implode</span>( "|", <span>array_keys</span>( <span>$week_day</span> ) ) . ")/u"<span>;
</span><span> 35</span>         <span>preg_match_all</span>( <span>$reg1</span>, <span>$content</span>, <span>$match</span><span> );
</span><span> 36</span>         <span>if</span>( (!<span>empty</span>( <span>$match</span>[0] )) && (!<span>empty</span>( <span>$match</span>[2] )) && (!<span>empty</span>( <span>$match</span>[3<span>] )) ){
</span><span> 37</span>             <span>//</span><span>匹配结果数组 1:下; 2:星期 3:一
</span><span> 38</span> <span>            //判断今天</span>
<span> 39</span>             <span>$w</span> = <span>date</span>( "w", <span>$now</span><span> );
</span><span> 40</span>             <span>$w</span> == 0 && <span>$w</span> = 7<span>;
</span><span> 41</span>             <span>//</span><span>判断输入时期</span>
<span> 42</span>             <span>$w_2</span> = <span>$week_day</span>[<span>$match</span>[3][0<span>]];
</span><span> 43</span>             <span>if</span>( <span>empty</span>( <span>$match</span>[1][0<span>] ) ){
</span><span> 44</span>                 <span>$time</span> = <span>$w_2</span> - <span>$w</span> > 0 ? 3600 * 24 * (<span>$w_2</span> - <span>$w</span>) : 0<span>;
</span><span> 45</span>             }<span>else</span><span>{
</span><span> 46</span>                 <span>$time</span> = 3600 * 24 * (<span>$w_2</span> - <span>$w</span> + 7<span> );
</span><span> 47</span> <span>            }
</span><span> 48</span> <span>        }
</span><span> 49</span>         <span>return</span> <span>$time</span> == 0 ? <span>false</span> : ( int ) <span>$now</span> + ( int ) <span>$time</span><span>;
</span><span> 50</span> <span>    }
</span><span> 51</span> 
<span> 52</span> <span>function</span> analyze_time_date( <span>$content</span><span> ){
</span><span> 53</span>         <span>$now</span> =<span> now();
</span><span> 54</span>         <span>$content</span> = <span>str_replace</span>( ".", "-", <span>$content</span><span> );
</span><span> 55</span>         <span>if</span>( <span>strtotime</span>( <span>$content</span><span> ) ){
</span><span> 56</span>             <span>return</span> <span>strtotime</span>( <span>$content</span><span> );
</span><span> 57</span> <span>        }
</span><span> 58</span> 
<span> 59</span>         <span>$number</span> = <span>array</span>( "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"<span> );
</span><span> 60</span>         <span>$number_2</span> = <span>$number</span><span>;
</span><span> 61</span>         <span>$number_2</span> = <span>array_splice</span>( <span>$number_2</span>, 1, 10<span> );
</span><span> 62</span>         <span>$reg2_2</span> = "/(0?[1-9]|10|11|12|[" . <span>implode</span>( "", <span>$number_2</span> ) . "]|(十一|十二))月([012]?[0-9]|30|31|(十|二十|三十)?[" . <span>implode</span>( "", <span>$number_2</span> ) . "十]?)日?/u"<span>;
</span><span> 63</span>         <span>preg_match_all</span>( <span>$reg2_2</span>, <span>$content</span>, <span>$match</span><span> );
</span><span> 64</span> <span>//</span><span>        print_r( $match );
</span><span> 65</span> <span>        //匹配数组 1:月份 3:日</span>
<span> 66</span> 
<span> 67</span>         <span>if</span>( (!<span>empty</span>( <span>$match</span>[0] )) && (!<span>empty</span>( <span>$match</span>[1][0] )) && (!<span>empty</span>( <span>$match</span>[3][0<span>] )) ){
</span><span> 68</span>             <span>$str_month</span> = <span>$match</span>[1][0<span>];
</span><span> 69</span>             <span>$str_day</span> = <span>$match</span>[3][0<span>];
</span><span> 70</span>             <span>$month</span> = <span>is_numeric</span>( <span>$str_month</span> ) ? <span>$str_month</span> : <span>false</span><span>;
</span><span> 71</span>             <span>if</span>( !<span>$month</span><span> ){
</span><span> 72</span>                 <span>foreach</span>( <span>$number</span> <span>as</span> <span>$k</span> => <span>$v</span><span> ){
</span><span> 73</span>                     <span>if</span>( <span>$v</span> == <span>$str_month</span><span> ){
</span><span> 74</span>                         <span>$month</span> = <span>$k</span><span>;
</span><span> 75</span> <span>                    }
</span><span> 76</span> <span>                }
</span><span> 77</span> <span>            }
</span><span> 78</span> <span>//</span><span>            echo "month:{$month}";</span>
<span> 79</span> 
<span> 80</span>             <span>$day</span> = <span>is_numeric</span>( <span>$str_day</span> ) ? <span>$str_day</span> : <span>false</span><span>;
</span><span> 81</span>             <span>if</span>( !<span>$day</span><span> ){
</span><span> 82</span>                 <span>$day</span> = <span>array_search</span>( <span>$str_day</span>, <span>$number</span><span> );
</span><span> 83</span>                 <span>if</span>( !<span>$day</span><span> ){
</span><span> 84</span>                     <span>$number_3</span> = <span>$number</span><span>;
</span><span> 85</span>                     <span>$number_3</span> = <span>array_splice</span>( <span>$number_3</span>, 1, 9<span> );
</span><span> 86</span>                     <span>$re_day</span> = <span>preg_match_all</span>( "/([一二三])?(十)([" . <span>implode</span>( "", <span>$number_3</span> ) . "])?/u", <span>$str_day</span>, <span>$match_2</span><span> );
</span><span> 87</span>                     <span>$day</span> = <span>array_search</span>( <span>$match_2</span>[1][0], <span>$number</span> ) ? <span>array_search</span>( <span>$match_2</span>[1][0], <span>$number</span> ) * 10 : 10<span>;
</span><span> 88</span>                     <span>$day</span> += <span>array_search</span>( <span>$match_2</span>[3][0], <span>$number</span> ) ? <span>array_search</span>( <span>$match_2</span>[3][0], <span>$number</span> ) : 0<span>;
</span><span> 89</span> <span>                }
</span><span> 90</span> <span>            }
</span><span> 91</span> <span>//</span><span>            echo "day:{$day}";</span>
<span> 92</span> 
<span> 93</span>             <span>if</span>( <span>$month</span> == <span>false</span> || <span>$day</span> == <span>false</span><span> ){
</span><span> 94</span>                 <span>return</span> <span>false</span><span>;
</span><span> 95</span> <span>            }
</span><span> 96</span> 
<span> 97</span>             <span>if</span>( <span>$match</span>[0][0] == <span>$content</span><span> ){
</span><span> 98</span>                 <span>//</span><span>无年份前缀</span>
<span> 99</span>                 <span>$output</span> = <span>date</span>( "Y", <span>$now</span> ) . "-" . <span>$month</span> . "-" . <span>$day</span><span>;
</span><span>100</span>             }<span>else</span><span>{
</span><span>101</span>                 <span>//</span><span>有年份前缀</span>
<span>102</span>                 <span>$year</span> = <span>$this</span> -> analyze_time_year( <span>str_replace</span>( <span>$match</span>[0][0], "", <span>$content</span><span> ) );
</span><span>103</span>                 <span>if</span>( <span>$year</span><span> ){
</span><span>104</span>                     <span>$output</span> = <span>$year</span> . "-" . <span>$month</span> . "-" . <span>$day</span><span>;
</span><span>105</span>                 }<span>else</span><span>{
</span><span>106</span>                     <span>return</span> <span>false</span><span>;
</span><span>107</span> <span>                }
</span><span>108</span> <span>            }
</span><span>109</span>             <span>return</span> <span>strtotime</span>( <span>$output</span><span> );
</span><span>110</span> <span>        }
</span><span>111</span>         <span>return</span> <span>false</span><span>;
</span><span>112</span> <span>    }
</span><span>113</span> 
<span>114</span> 
<span>115</span> <span>function</span> analyze_time_year( <span>$content</span><span> ){
</span><span>116</span>         <span>$number</span> = <span>array</span>( "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"<span> );
</span><span>117</span> 
<span>118</span> 
<span>119</span>         <span>//</span><span>模式二 --一,匹配年 2001年,01年,零一年,二九年,二零一九年
</span><span>120</span> <span>        //1-9</span>
<span>121</span>         <span>$number_1</span> = <span>$number</span><span>;
</span><span>122</span>         <span>$number_1</span> = <span>array_splice</span>( <span>$number_1</span>, 0, 10<span> );
</span><span>123</span>         <span>$reg2_1</span> = "/((20|二零)?([12一二])([0-9]|[" . <span>implode</span>( "", <span>$number_1</span> ) . "]))(?(1)年)/u"<span>;
</span><span>124</span>         <span>preg_match_all</span>( <span>$reg2_1</span>, <span>$content</span>, <span>$match</span><span> );
</span><span>125</span>         <span>if</span>( (!<span>empty</span>( <span>$match</span>[0] )) && (!<span>empty</span>( <span>$match</span>[3][0] )) && (!<span>empty</span>( <span>$match</span>[4][0<span>] )) ){
</span><span>126</span>             <span>$day</span> = ( <span>is_numeric</span>( <span>$match</span>[3][0] ) ? <span>$match</span>[3][0] : <span>array_search</span>( <span>$match</span>[3][0], <span>$number</span> ) ) * 10<span>;
</span><span>127</span>             <span>$day</span> += <span>is_numeric</span>( <span>$match</span>[4][0] ) ? <span>$match</span>[4][0] : <span>array_search</span>( <span>$match</span>[4][0], <span>$number</span><span> );
</span><span>128</span>         }<span>else</span><span>{
</span><span>129</span>             <span>return</span> <span>false</span><span>;
</span><span>130</span> <span>        }
</span><span>131</span> 
<span>132</span>         <span>return</span> <span>$day</span> ? <span>$day</span> + 2000 : <span>false</span><span>;
</span><span>133</span>     }

每个函数都可以单独使用。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn