Retrieve email using regex
ThisĀ horrendousĀ regular expression will parse a string and return a valid email address from it. $email = “<’Freddy’> fred@live.com”; preg_match(‘/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/’, $email, $match); echo $match[0]; This will return: fred@live.com Basically, if you pass an array as the third parameter of the preg_match method, it will be filled with the preg_match results, and the first item of the [...]
Online regex -> php tool
I’ve found an excellent online tool, where you can give it a regex, and it will spit out the PHP syntax for use with preg_replace, preg_match, etc. I’m forever getting ‘Unknown modifier’ errors when trying to insert regex’s into my PHP code. This sorts it out for you. http://regex.larsolavtorvik.com/