如何在Php中分离电子邮件和号码

时间:2014-06-19 06:39:55

标签: php

我需要什么:

解析一个字符串,如:

purchasing@is.com3940602685554

我面临的问题:

  

I cannot parse domain name and number

到目前为止我尝试过:

$myString = ' purchasing@isdaf.com3940602685554 ';
$myArray = explode('.', $myString);
print_r($myArray);

我的目标:

HTML code:

   <select multiple id="e2" name="team[]" style="width:300px">
   <option value=" purchasing@is.com3940602685554 ">Purchasing</option>

我必须插入域名并输入另一个字段。

所以我的查询就像:

    $team=$_POST['team'];
    teamname=mysql_real_escape_string($_POST['teamname']);

   foreach ($team as $t) {
    $query = "INSERT INTO teamname(People_Name,User_id,Team_Name) VALUES ('".$t[0]."','".$t[1]."','".$teamname."')" ;
  }

3 个答案:

答案 0 :(得分:3)

你应该尝试正则表达式

$string = "purchasing@is.com3940602685554";
preg_match('/^(.+\.[a-z]{2,4})([0-9]+)$/', $string, $matches);
$email = $matches[1]; //purchasing@is.com
$number = $matches[2]; //3940602685554

或者,如果您只想尝试域名

$string = "purchasing@is.com3940602685554";
preg_match('/^.+\@([a-z0-9]+\.[a-z]{2,4})([0-9]+)$/', $string, $matches);
$domain = $matches[1]; //is.com
$number = $matches[2]; //3940602685554

echo $domain;
echo $number;

如果你想插入DB试试

foreach ($team as $t) {
    preg_match('/^(.+\.[a-z]{2,4})([0-9]+)$/', $t, $matches);
    $email = $matches[1];
    $number = $matches[2];

    $query = "INSERT INTO teamname(People_Name,User_id,Team_Name) VALUES('".$email."','".$number."','".$teamname."')" ;
}

答案 1 :(得分:1)

尝试 preg_match()

$str = 'purchasing@is.com3940602685554';
 preg_match_all('/([a-zA-Z@.]+)([0-9]+)/', $str, $matches);
echo  $domain = $matches[1][0];//urchasing@is.com
echo  $number = $matches[2][0]; //3940602685554

 $str = 'purchasing@is.com3940602685554';
 preg_match('/([a-zA-Z@.]+)([0-9]+)/', $str, $matches);
 echo  $domain = $matches[1]; //urchasing@is.com
 echo  $number = $matches[2]; //3940602685554

答案 2 :(得分:-1)

使用此代码。如果您只定位.com

  $string = "purchasing@is.com3940602685554";
preg_match('/^(.+\.[a-z]{2,4})([0-9]+)$/', $string, $matches);
$emailID = $matches[1];
$Contact = $matches[2];

echo $emailID."<br/>";
echo $Contact;