SuperMykEl SuperMykEl - 3 months ago 17
PHP Question

PHP preg_match_all regex expression weirdness

I am having some trouble with regex in php (preg_match_all).

I am using the following code to find an email encapsulated by <>:, i.e. :

preg_match_all("<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:", $body,$matches);


For some reason PHP is blowing up at the colon with the following error:


Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ':' in...


Any help would be much appreciated, as I am no regex guru, and am just about out of hair to pull.

Answer

You need to use delimeters EX:

preg_match_all('/<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:/', $body,$matches);

See the / I added on both ends telling PHP where the regex starts and ends.

Comments