Pieter
Pieter

Reputation: 32755

Using regex in PHP

I tried this:

$mtcDatum = preg_match("/[0-9]{2}/[0-9]{2}/[0-9]{4}/", $lnURL);

This returns an error message:

Warning: preg_match() [function.preg-match]: Unknown modifier '['

Why doesn't this work? I'm used to Linux's way of doing regex, does PHP handle regex differently?

Upvotes: 0

Views: 127

Answers (2)

Ben James
Ben James

Reputation: 125139

You need a delimiter character around your pattern (this is what separates the pattern from any modifiers). Normally / is used, but as this is part of the string you are trying to match, you can use another character such as #:

$mtcDatum = preg_match("#[0-9]{2}/[0-9]{2}/[0-9]{4}#", $lnURL);

Upvotes: 2

davearchie
davearchie

Reputation: 317

PHP syntax is interpreting the "/" character as the end of your pattern. You need to escape the forward slashes:

preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/", $lnURL);

Upvotes: 5

Related Questions