SoFunction
Updated on 2025-03-02

A useful method to match the phone number, mobile number, email address

Find a useful method to match phone numbers and mobile phone numbers with a good regular basis. Thank you. I will share it here. I hope it can help more people

Match all URL addresses and link content
Copy the codeThe code is as follows:

$str = "<a href='' target='_blank'>Baidu</a>http://www<a href='' target='_blank'>Sina</a>kod";

preg_match_all("/<a href=([\"\'])(http:\/\/([\w\d\.])+)[^>]*>(.*?)<\/a>/i", $str, $matches);

echo "<pre>";
var_dump($matches[2]);
echo "<br />";
var_dump($matches[4]);

Match email
Copy the codeThe code is as follows:

$emails = "[email protected]@";

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 codeThe code is 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);