Jump to content

Grabbing everything before the @


9three

Recommended Posts

Hi,

 

I was wondering how I would be able to grab characters from an email all the way up to the @ symbol?

 

Example:

 

foo1@bar.com

 

The result I would need would be:

 

foo1

 

This would match what I'm looking for?

/^[A-Z0-9._%-]+@$/i

 

And then do something like:

 

preg_match('/^[A-Z0-9._%-]+@$/i', $email, $match);

echo $match[0];// Prints everything up to the @

Link to comment
Share on other sites

I'm not sure what your question is as you seem to have answered it yourself. In the example you gave though $match[0] will also contain the @ sign, not just the bit before it. Are you trying to extract the first part of an e-mail from a string you know to be an e-mail, or are you attempting to extract the first part of e-mails from a large piece of text?

Link to comment
Share on other sites

I know that, but you still didn't really answer my question, so I'll go ahead and assume $email only contains an e-mail. You will simply need to add a capture group to capture the part before the @. Personally though (assuming the e-mail has been validated) I'd use....

 

preg_match('/^[^@]+/i', $email, $match);
echo $match[0];

 

Which simply says from the start of the string, grab everything untill you encounter an @ symbol

Link to comment
Share on other sites

$email = "big.nobody@example.com";

if( preg_match("/^(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*)@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email,$matches)) {

	           print_r($matches);

	 }

 

 

Prints:

Array
(
    [0] => big.nobody@example.com
    [1] => big.nobody
    [2] => big
    [3] => .nobody
    [4] => example.
)

 

Link to comment
Share on other sites

The following example looks for one or more non-@ characters from the start of the string. (Same as cags's but without the case-insensitive modifier i since that's not necessary)

 

$email = "foo@example.org";
preg_match('/^[^@]+/', $email, $match);
echo $match[0]; // foo

Link to comment
Share on other sites

Ahhh interesting!

 

There are so many email patterns out there.... which one to use... which one to use...  :o

 

ahem, I borrowed that one from Codeignitor. Been keeping it my can of things for awhile.  :D

 

What you probably dont understand is your missing the logic of the  ()  Every ()  will create a new index in the array.  You could have this.  preg_match('#(.*)@(.*)#',$email,$matches) 

$matches[1] would be the first () and would contain everything up to the @.  (But dont use that regex its really bad.)

Link to comment
Share on other sites

Damn you salathe.... lol, the i was only there because I copied the OP's code to edit and never thought to remove it. One of these days I'll get one right. :)

 

Note. In no way does the pattern put forward by myself and salathe validate e-mail, it simply does what you asked, finds the part before the @ sign in a string that should be an e-mail.

Link to comment
Share on other sites

$email = "foo@example.org";
preg_match('/^[^@]+/', $email, $match);
echo $match[0]; // foo

 

 

This will also match this.

 

&#(*$&(*#&$(&(#&$(&(#&$&@example.com

I would only use that if you pulling the email from a database and are already sure it was validated before being put into the database.

Link to comment
Share on other sites

$email = "foo@example.org";
preg_match('/^[^@]+/', $email, $match);
echo $match[0]; // foo

 

 

This will also match this.

 

&#(*$&(*#&$(&(#&$(&(#&$&@example.com

I would only use that if you pulling the email from a database and are already sure it was validated before being put into the database.

 

As I already said, the pattern was never intended to validate e-mails. At no point did the OP suggest that was required, the pattern provided did what they asked, select the string before the @ sign.

Link to comment
Share on other sites

/^(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*)@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix

 

If going the 'tight-fisted' email format route, I would recommend having a look at iamcal's email parser (there's a complete RFC822 compliant email address matcher link to download at the bottom of that page, which leads to the RFC 3696 Parser - Be warned, it's more heavy than your simply run of the mill regex email pattern, but seems quite robust). The site even lists a table showing the different RFC paresers and which email formats will pas under which version used.

 

On a side note, most characters within a character class are treated as literals (there are some characters in some circumstances that retain meta character status, depending on their location within the class). So stuff like + don't need to be escaped. Nor does the dash need to be escaped on the condition that it is listed as the very first or very last character within the class. I'm not sure why the use of the freeform x modifier, as it doesn't serve anything in this case.. If there is a newline at the end of the email being checked, I would make use of the D modifier instead.

Link to comment
Share on other sites

Be warned, it's more heavy than your simply run of the mill regex email pattern, but seems quite robust).

 

You ain't kidding man, what the hell. This is valid?

"first\\last"@example.com

The request for comments idea of what a valid email is border on crazyness. Well along as they provide the insane validation function, all is good. :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.