0
$regex = '([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})';
$string = 'I am emailing to john@gmail.com and hoe@gmail.com but harris@live.com';

$newString = preg_replace($regex,'',$string);

我想用空字符串替换所有电子邮件地址,留下前两个。所以 $newString 应该是 I am emailing to john@gmail.com and hoe@gmail.com but. 但徒劳无功。

我该怎么做....

4

2 回答 2

1

作为通用答案:

  • preg_split与 一起使用PREG_SPLIT_DELIM_CAPTURE

  • 在结果数组上,删除每个索引不均匀的数组项,从[4].

  • 然后使用 连接余数implode()

于 2013-04-06T22:55:22.503 回答
-1

你可以尝试使用爆炸

 $str = I am emailing to john@gmail.com and hoe@gmail.com but harris@live.com; 
 $str_array = explode( , $str); 
 $counter = 0; 
 $str_out = ; 
 foreach($str_array as $cast){ 
    $findme   = @; 
    $pos = strpos($cast, $findme); 
    if ($pos === false) { 
        $str_out = $str_out . $cast .  ; 
    }else{ 
        $counter++; 
        if($counter<=2){ 
            $str_out = $str_out . $cast .  ; 
        } 
    } 
 } 
 echo $str_out;  
于 2013-04-06T23:14:12.373 回答