Home > Article > Web Front-end > How to use regular expressions to match phone numbers and email addresses
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='http://www.baidu.com' target='_blank'>百度</a>http://www<a href='http://www.sina.com' target='_blank'>新浪</a>kod"; preg_match_all("/<a href=([\"\'])(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!