如何删除邮件格式容器标记以外的标记

时间:2012-09-03 10:53:03

标签: php regex email replace

用文字描述我的问题很困难,我会尝试举例:

str = '<p>lorem ipsum <xyz@abc.com> donor sit <br></p>';

我需要删除除 <xyz@abc.com>

以外的所有代码

我们如何在javascript和PHP中做到?

我的PHP解决方案:

class test {
    public function keepMailAddresses($text){
       $callBack = array($this,'_keepMailAddresses');
       return preg_replace_callback('/(<)([^0-9][a-zA-Z0-9_]*([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4})(>)/i', $callBack, $text);        
    }

    private function _keepMailAddresses($matches){
       return '&lt;'.$matches[2].'&gt;';
    }

}

$obj = new test();
echo $obj->keepMailAddresses('<p>lorem ipsum <xyz@abc.com> donor sit <br></p>');

2 个答案:

答案 0 :(得分:1)

好吧,我的解决方案有点奇怪,但会做到这一点:D

$pagecode = '<p>lorem ipsum <xyz@abc.com> donor <abc_def.xyz@abc.com> sit <abc_def.xyz@abc.abc.com><br></p>';

// this will check if it's a real email but you don't need it
/*$allowed = preg_match_all("/\<+([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})+\>/i", $pagecode, $matches);*/

$allowed = preg_match_all("/\<([_a-z0-9-\.]+)@([_a-z0-9-\.]+)\>/i", $pagecode, $matches);

$allowed = implode(" ", $matches[0]);
$output = strip_tags($pagecode, $allowed);
echo htmlentities($output);

答案 1 :(得分:1)

在标题部分

中使用以下内容
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

并像这样放

str = '<p>lorem ipsum "<xyz@abc.com>" donor sit <br></p>';