PHP惯用验证正则表达式

WBOY
Freigeben: 2016-06-13 12:23:57
Original
996 Leute haben es durchsucht

PHP常用验证正则表达式

数字、手机号、QQ号、Url地址合法性校验

1.验证是否为整数

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> isNumber(<span style="color: #800080;">$val</span><span style="color: #000000;">)</span><span style="color: #008080;">2</span> <span style="color: #000000;">    {</span><span style="color: #008080;">3</span>             <span style="color: #0000ff;">if</span>(<span style="color: #008080;">ereg</span>("^[0-9]+$", <span style="color: #800080;">$val</span><span style="color: #000000;">))</span><span style="color: #008080;">4</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">5</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">6</span>      }
Nach dem Login kopieren

2.验证是否为手机号

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> isPhone(<span style="color: #800080;">$val</span><span style="color: #000000;">){</span><span style="color: #008080;">2</span>             <span style="color: #008080;">3</span>           <span style="color: #0000ff;">if</span> (<span style="color: #008080;">ereg</span>("^1[1-9][0-9]{9}$",<span style="color: #800080;">$val</span><span style="color: #000000;">))</span><span style="color: #008080;">4</span>           <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">5</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">6</span>     <span style="color: #008080;">7</span>           <span style="color: #008080;">8</span>     }
Nach dem Login kopieren

3.验证是否为有效的QQ号

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> isQQ(<span style="color: #800080;">$val</span><span style="color: #000000;">){</span><span style="color: #008080;">2</span>             <span style="color: #008080;">3</span>           <span style="color: #0000ff;">if</span> (<span style="color: #008080;">ereg</span>("^[1-9][0-9]{4,12}$",<span style="color: #800080;">$val</span><span style="color: #000000;">))</span><span style="color: #008080;">4</span>           <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">5</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">6</span>     <span style="color: #008080;">7</span>           <span style="color: #008080;">8</span>     }
Nach dem Login kopieren

4.验证是否是合法的Url地址

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> isUrl(<span style="color: #800080;">$s</span><span style="color: #000000;">)  {  </span><span style="color: #008080;"> 2</span>         <span style="color: #0000ff;">return</span> <span style="color: #008080;">preg_match</span>('/^http[s]?:\/\/'.  <span style="color: #008080;"> 3</span>             '(([0-9]{1,3}\.){3}[0-9]{1,3}'. <span style="color: #008000;">//</span><span style="color: #008000;"> IP形式的URL- 199.194.52.184  </span><span style="color: #008080;"> 4</span>             '|'. <span style="color: #008000;">//</span><span style="color: #008000;"> 允许IP和DOMAIN(域名)  </span><span style="color: #008080;"> 5</span>             '([0-9a-z_!~*\'()-]+\.)*'. <span style="color: #008000;">//</span><span style="color: #008000;"> 域名- www.  </span><span style="color: #008080;"> 6</span>             '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.'. <span style="color: #008000;">//</span><span style="color: #008000;"> 二级域名  </span><span style="color: #008080;"> 7</span>             '[a-z]{2,6})'.  <span style="color: #008000;">//</span><span style="color: #008000;"> first level domain- .com or .museum  </span><span style="color: #008080;"> 8</span>             '(:[0-9]{1,4})?'.  <span style="color: #008000;">//</span><span style="color: #008000;"> 端口- :80  </span><span style="color: #008080;"> 9</span>             '((\/\?)|'.  <span style="color: #008000;">//</span><span style="color: #008000;"> a slash isn't required if there is no file name  </span><span style="color: #008080;">10</span>             '(\/[0-9a-zA-Z_!~\'\(\)\[\]\.;\?:@&=\+\$,%#-\/^\*\|]*)?)$/',  <span style="color: #008080;">11</span>             <span style="color: #800080;">$s</span>) == 1<span style="color: #000000;">;  </span><span style="color: #008080;">12</span>     }  
Nach dem Login kopieren

使用方式很简单,如验证手机号是否有效:

<span style="color: #800080;">$val</span>='15093691582'<span style="color: #000000;">;isPhone(</span><span style="color: #800080;">$val</span>);
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage