dsf6565 2012-04-15 09:29
浏览 4
已采纳

使用PHP检查电子邮件

I'm using this simple code:

$raw = 'text hi@li.com text';
$raw = preg_replace('<[\w.]+@[\w.]+>', '***@$2', $raw);

And i should get as output, something like ***@li.com; while i get ***@

I can't debug it, i don't know how what's wrong.


So the solution is

preg_replace('<([\w.]+)@([\w.]+)>', '***@$2', $raw);

I had to add () to make a group.

  • 写回答

5条回答 默认 最新

  • douzong2206 2012-04-15 09:33
    关注

    you need to create a group by adding (), and BTW it's gonna be $1:

    $raw = ' hi@li.com ';
    $raw = preg_replace('/[^@]+@([^\s]+)/', '***@$1', $raw);
    

    also modified .+ tp [^\s]+ so it "eats" only the email and not the text after it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?