inactive Posted February 26, 2008 Share Posted February 26, 2008 hey guys just doing my first preg_match to look for a valid email address. im using "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$^" which works, but the thing is i need it to pick out an email out of a csv-like string. i.e. the sting may be: "foo","bar","foobar","foo@bar.com" and i need it to pick out the foo@bar.com, and just cant work out how to do it. any ideas? probably a kinda lame question i know, but all the regex tutes i find are so complex... cheers. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 If you are reading from a CSV file, you should check out http://us.php.net/fgetcsv, it will do all the data parsing for you. Then you can use your regex to test the email address. Quote Link to comment Share on other sites More sharing options...
inactive Posted February 26, 2008 Author Share Posted February 26, 2008 tks rhodesa, very helpful idea. but out of curiosity, does anyone know how i would do what i wanted without fgetscsv? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 There are lots of ways. Here is one: <?php $string = '"foo","bar","foobar","foo@bar.com"'; list(,,,$email) = explode(',',$string,4); $email = trim($email,'"'); print $email; ?> Quote Link to comment Share on other sites More sharing options...
inactive Posted February 26, 2008 Author Share Posted February 26, 2008 yeah thats what i ended up using (roughly). what about using preg_match? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 you could use: <?php $string = '"foo","bar","foobar","foo@bar.com"'; if(preg_match('/"([^"]+)"$/',$string,$matches)){ $email = $matches[1]; print $email; } ?> Quote Link to comment Share on other sites More sharing options...
inactive Posted February 26, 2008 Author Share Posted February 26, 2008 cheers. Quote Link to comment Share on other sites More sharing options...
inactive Posted October 29, 2014 Author Share Posted October 29, 2014 After splitting the CSV into an array, you can iterate through it and check for an email with filter_var($email, FILTER_VALIDATE_EMAIL) 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.