什么是preg_match_all获取域名的模式?

时间:2011-11-20 06:04:00

标签: php preg-match-all

我想从文本中获取域名,我可以轻松地为电子邮件做,但无法获取域名。我使用此代码获取电子邮件地址:

if (!empty($text)) {
  $res = preg_match_all(
    "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
    $text,
    $matches
  );
}

从文本中获取域名的模式是什么?

2 个答案:

答案 0 :(得分:1)

//if you mean to get domain from any url then this works:
preg_match( '@^(?:http://)?([^/]+)@i', $text, $matches );
$host = $matches[1];

//if you mean to get domain from email address , then this should work
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
{
list($username,$domain)=split('@',$email);
echo "Domain is:'.$domain;

希望这是有帮助的

答案 1 :(得分:0)

在进行电子邮件地址验证时,可以在此处找到最强大的RegEx: http://fightingforalostcause.net/misc/2006/compare-email-regex.php

相关问题