duanba8070 2017-09-16 04:19 采纳率: 0%
浏览 41
已采纳

Php使用正则表达式查找字符串的一部分

I want to get a the part of string of an email after @ using regular expression. For example, I have the string 'mirko@gmail.com' and i want to get 'gmail.com'. I wrote /^@./ but doesn't work- I wrote

preg_match('/^@./', $string, $output);
print($output);

How can i fix it?

  • 写回答

2条回答 默认 最新

  • dqpc1845 2017-09-16 04:25
    关注

    Why regex for this kind of simple task? use strpos with the combination of substr or just used explode() with @ as first parameter as directed by other answers.

    $email_string = "mirko@gmail.com";    
    $result = substr($email_string, strpos($email_string, "@") + 1);    
    echo $result ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?