Home  >  Article  >  Web Front-end  >  How to use regular expressions to match phone numbers and email addresses

How to use regular expressions to match phone numbers and email addresses

php中世界最好的语言
php中世界最好的语言Original
2018-01-08 10:05:531901browse

This time I will show you how to use regular expressions to match phone numbers, mobile numbers, and email addresses. How to use regular expressions to match phone numbers, mobile numbers, and email addresses? What are the precautions for using regular expressions to match phone numbers, mobile phone numbers and email addresses? The following is a practical case, let’s take a look.

Match all url addresses and link content

Copy the code The code is as follows:

$str = "<a href=&#39;http://www.baidu.com&#39; target=&#39;_blank&#39;>百度</a>http://www<a href=&#39;http://www.sina.com&#39; target=&#39;_blank&#39;>新浪</a>kod"; 
preg_match_all("/<a href=([\"\&#39;])(http:\/\/([\w\d\.])+)[^>]*>(.*?)<\/a>/i", $str, $matches); 
echo "<pre class="brush:php;toolbar:false">"; 
var_dump($matches[2]); 
echo "<br />"; 
var_dump($matches[4]);

Match the email

Copy the code The code is as follows:

$emails = "wangzhanu@126.comha12wangzhanu@163.com"; 
preg_match_all("/([a-z0-9_\-\.]+)@(([a-z0-9]+[_\-]?)\.)+[a-z]{2,3}/i", $emails, $matches); 
var_dump($matches[0]);

Match mobile phone and phone number

Copy the code code as follows:

$tel = "13911112222sf010-44444442dfg18811112222"; 
preg_match_all("/1[3,5,8]{1}[0-9]{1}[0-9]{8}|0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{1,4})?/", $tel, $matches); 
var_dump($matches);


I believe you have mastered it after reading these cases For more exciting methods, please pay attention to php Chinese websiteOther related articles!

Related reading:

How to use the template of the WeChat applet

How to use jquery's ajax to asynchronously submit form data

What is the order in which JS code is executed?

The above is the detailed content of How to use regular expressions to match phone numbers and email addresses. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn