PHP常用验证正则表达式,php正则表达式_PHP教程

WBOY
Libérer: 2016-07-13 09:44:48
original
1031 Les gens l'ont consulté

PHP常用验证正则表达式,php正则表达式

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

1.验证是否为整数

<span>1</span> <span>function</span> isNumber(<span>$val</span><span>)
</span><span>2</span> <span>    {
</span><span>3</span>             <span>if</span>(<span>ereg</span>("^[0-9]+$", <span>$val</span><span>))
</span><span>4</span>                 <span>return</span> <span>true</span><span>;
</span><span>5</span>             <span>return</span> <span>false</span><span>;
</span><span>6</span>      }
Copier après la connexion

2.验证是否为手机号

<span>1</span> <span>function</span> isPhone(<span>$val</span><span>){
</span><span>2</span>             
<span>3</span>           <span>if</span> (<span>ereg</span>("^1[1-9][0-9]{9}$",<span>$val</span><span>))
</span><span>4</span>           <span>return</span> <span>true</span><span>;
</span><span>5</span>         <span>return</span> <span>false</span><span>;
</span><span>6</span>     
<span>7</span>           
<span>8</span>     }
Copier après la connexion

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

<span>1</span> <span>function</span> isQQ(<span>$val</span><span>){
</span><span>2</span>             
<span>3</span>           <span>if</span> (<span>ereg</span>("^[1-9][0-9]{4,12}$",<span>$val</span><span>))
</span><span>4</span>           <span>return</span> <span>true</span><span>;
</span><span>5</span>         <span>return</span> <span>false</span><span>;
</span><span>6</span>     
<span>7</span>           
<span>8</span>     }
Copier après la connexion

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

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

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

<span>$val</span>='15093691582'<span>;
isPhone(</span><span>$val</span>);
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1048725.htmlTechArticlePHP常用验证正则表达式,php正则表达式 数字、手机号、QQ号、Url地址合法性校验 1.验证是否为整数 1 function isNumber( $val ) 2 { 3 if ( ereg ("^[0-...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
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
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!