php常用Regex函數

來源:互聯網
上載者:User
php教程常用Regex函數
我們這裡面很多中Regex代碼,包括有判斷中文正則,數字字母正則,字元正則表達試,數字正則表達試,郵箱Regex,電話號碼正則表達試,手機號碼正則表達試,郵編正則表達試,/url正則表達試等哦。
*/
 function funcchinese($str,$num1='',$num2='')//判斷中文正則
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^([x81-xfe][x40-xfe]){".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (!eregi("[^x80-xff]","$str"))?true:false;
  }
 }
 
 function funcstrnum($str,$num1='',$num2='') //數字字母正則
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^[^0-9a-za-z_@!.-]{".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (preg_match("/^[^0-9a-za-z_@!.-]/",$str))?true:false;
  }  
 }
 // 常用的正則表達試
 
 function funcstr($str,$num1='',$num2='') //字元正則表達試
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^[a-za-z]{".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (preg_match("/^[a-za-z]/",$str))?true:false;
  }  
 }
 
 function funcnum($str,$num1='',$num2='')//數字正則表達試
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^[0-9]{".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (preg_match("/^[0-9]/",$str))?true:false;
  }
 }
 
 function funccard($str)//
 {
  return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false;
 }
 
 function funcemail($str)//郵箱Regex
 {
  return (preg_match('/^[_.0-9a-z-a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str))?true:false;
 }
 
 function funcphone($str)//電話號碼正則表達試
 {
  return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,8}$/",$str))?true:false;
 }    
 
 function funcmtel($str)//手機號碼正則表達試
 {
  return (preg_match("/(?:13d{1}|15[03689])d{8}$/",$str))?true:false;
 } 
 
 function funczip($str)//郵編正則表達試
 {
  return (preg_match("/^[0-9]d{5}$/",$str))?true:false;
 } 
 
 function funcurl($str)//url正則表達試
 {
  return (preg_match("/^http://[a-za-z0-9]+.[a-za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str))?true:false;
 } 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.