Page 1 of 1

PHP remove name and < > from email address

Posted: Sat Mar 31, 2012 2:02 pm
by cjkeane
Hi everyone,

Is it possible in an email address such as: John Smith <johnsmith@home.ca> to remove everything before the first < and then remove the last > so that the email address becomes: johnsmith@home.ca?

Thanks.

Re: PHP remove name and < > from email address

Posted: Sat Mar 31, 2012 2:23 pm
by Celauran
Untested, but something like this should work.

Code: Select all

$email = preg_replace('/.*</', '', $email);
$email = rtrim($email, '>');

Re: PHP remove name and < > from email address

Posted: Sat Mar 31, 2012 2:48 pm
by cjkeane
it works however I had to modify it slightly:

$email = preg_replace('/.*</', '', $email);
$email = rtrim($email, '>');
echo $email;

thanks for the tip.