I have an email address that could either be
$email = "x@example.com";
or $email="Johnny <x@example.com>"
I want to get
$handle = "x";
for either version of the $email.
How can this be done in PHP (assuming regex). I'm not so good at regex.
Thanks in advance
Use the regex
<?([^<]+?)@
then get the result from$matches[1]
.Here's what it does:
<?
matches an optional<
.[^<]+?
does a non-greedy match of one or more characters that are not^
or<
.@
matches the@
in the email address.A non-greedy match makes the resulting match the shortest necessary for the regex to match. This prevents running past the
@
.Rubular: http://www.rubular.com/r/bntNa8YVZt