9three Posted October 25, 2009 Share Posted October 25, 2009 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 @ Quote Link to comment Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 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? Quote Link to comment Share on other sites More sharing options...
9three Posted October 25, 2009 Author Share Posted October 25, 2009 I'm trying to extract the first part of the email without the @ symbol. Are you saying basically just do this: preg_match('/^[A-Z0-9._%-]$/i', $email, $match); echo $match[0];// Prints the first part of the email Quote Link to comment Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 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 Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 25, 2009 Share Posted October 25, 2009 $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. ) Quote Link to comment Share on other sites More sharing options...
9three Posted October 25, 2009 Author Share Posted October 25, 2009 Ahhh interesting! There are so many email patterns out there.... which one to use... which one to use... Quote Link to comment Share on other sites More sharing options...
salathe Posted October 25, 2009 Share Posted October 25, 2009 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 Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 25, 2009 Share Posted October 25, 2009 Ahhh interesting! There are so many email patterns out there.... which one to use... which one to use... ahem, I borrowed that one from Codeignitor. Been keeping it my can of things for awhile. 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.) Quote Link to comment Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 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. Quote Link to comment Share on other sites More sharing options...
salathe Posted October 25, 2009 Share Posted October 25, 2009 Note. In no way does the pattern put forward by myself and salathe validate e-mail… Neither does keldorn's, to be fair. Nor should they. Quote Link to comment Share on other sites More sharing options...
9three Posted October 25, 2009 Author Share Posted October 25, 2009 thanks everyone. Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 25, 2009 Share Posted October 25, 2009 $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. Quote Link to comment Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 $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. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 25, 2009 Share Posted October 25, 2009 /^(([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. Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 25, 2009 Share Posted October 25, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 echo strstr($email, '@', true); Edit: Ha... maybe I should check date stamps before I post. Well, whatever. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.