Search code examples
phpregexpreg-match

Php find a part of string using regular expression


I want to get a the part of string of an email after @ using regular expression. For example, I have the string '[email protected]' 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?


Solution

  • 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 = "[email protected]";    
    $result = substr($email_string, strpos($email_string, "@") + 1);    
    echo $result ;