PHP remove name and < > from email address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP remove name and < > from email address

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP remove name and < > from email address

Post by Celauran »

Untested, but something like this should work.

Code: Select all

$email = preg_replace('/.*</', '', $email);
$email = rtrim($email, '>');
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP remove name and < > from email address

Post by cjkeane »

it works however I had to modify it slightly:

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

thanks for the tip.
Post Reply