rckehoe Posted April 24, 2009 Share Posted April 24, 2009 I have a very simple issue that I cannot figure out, I really suck at regular expressions... I have a bit of text formatted like so: First Last Name <Email@TheirEmail.com> I want to extract the Email Address from the < and >, how is this done? I am using this code, but obviously it is wrong! $SearchEmail = eregi("[<(.)>]", $r, $regs); Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/ Share on other sites More sharing options...
Maq Posted April 24, 2009 Share Posted April 24, 2009 To extract the WHOLE string in between the tags you can use: [] Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/#findComment-818386 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 I would avoid using eregi as it is depreciated in PHP6. Use preg_match instead. $string = "First Last Name <Email@TheirEmail.com>"; preg_match("~<(.*?)>~i", $string, $match); echo $match[1]; Outputs: Email@TheirEmail.com Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/#findComment-818389 Share on other sites More sharing options...
rckehoe Posted April 24, 2009 Author Share Posted April 24, 2009 When I outpint the $regs, all it outputs is a < ... It doesn't pull the email address. Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/#findComment-818390 Share on other sites More sharing options...
rckehoe Posted April 24, 2009 Author Share Posted April 24, 2009 Ok.. the preg_match worked... thanks! Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/#findComment-818392 Share on other sites More sharing options...
nrg_alpha Posted April 24, 2009 Share Posted April 24, 2009 [<(.+)>] Post #11 and 14 from this thread provide a caveat to using .+ (or even .*, same principal). If <Email@TheirEmail.com> is the complete string in itself, then it's not a big deal at all to use greedy quantifiers..(in fact, would be even preferable to do so, as backtracking would be minimal) but if this is all nested in a larger body of text, it would be wise to avoid such greedy quantifiers and use lazy quantifiers instead. $string = "First Last Name <Email@TheirEmail.com>"; preg_match("~<(.*?)>~i", $string, $match); Just to note that in this case, the i modifier isn't necessary, as the dot match all will match anything (including upper / lowercase alpha characters) other than a newline (that is, in the absence of the s modifier). Another alternative to lazy quantifiers include the use of a negated character class such as: ~<([^>]+)>~ Quote Link to comment https://forums.phpfreaks.com/topic/155522-solved-very-simple-eregi-issue/#findComment-818597 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.