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 protected]> 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); 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: [] 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 protected]>"; preg_match("~<(.*?)>~i", $string, $match); echo $match[1]; Outputs: [email protected] 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. 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! 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 protected]> 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 protected]>"; 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: ~<([^>]+)>~ 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
Archived
This topic is now archived and is closed to further replies.