PHP - preg_match and "Unknown modifier" error [duplicate]
Asked Answered
K

3

7

I had that test that worked fine :

if (ereg("([0-9]{2})[-./]([0-9]{2})[-./]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})", $dateToTest, $tab) == false)

and as ereg is deprecated, I have replaced that test with this one :

if (preg_match("/([0-9]{2})[-./]([0-9]{2})[-./]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})/", $dateToTest, $tab) == false)

But I get the following error :

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

What is the problem and how may I solve it ?

Kall answered 6/12, 2011 at 13:32 Comment(0)
F
10

The problem is the delimiter / because you use it in your regexp again.

You have to escape it \/ or use another delimiter like @:

if (preg_match("@([0-9]{2})[-/.]([0-9]{2})[-/.]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})@", $dateToTest, $tab) == false)

See the example #3 in the Docu. There is also a manual about delimiters.

Faultfinding answered 6/12, 2011 at 13:35 Comment(1)
I added a link to the docu about delimiters.Faultfinding
T
4

You have unescaped slashes in the expression. Either change / to \/ or use a different delimiter such as @ to start the expression.

Trike answered 6/12, 2011 at 13:34 Comment(0)
M
0

the error is here /.. i don't know what do you mean by this regexp, so you can change it to . or \.

Metry answered 6/12, 2011 at 13:35 Comment(2)
That seems evident : I test a date-time format, that can have 3 separators for the date : [- . /]Kall
@Kall oh, sorry. it's the end of my working day :)Metry

© 2022 - 2024 — McMap. All rights reserved.