PHP - preg_match and "Unknown modifier" error

19,426

Solution 1

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.

Solution 2

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

Share:
19,426
Oliver
Author by

Oliver

Updated on July 15, 2022

Comments

  • Oliver
    Oliver over 1 year

    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 ?

  • Oliver
    Oliver almost 12 years
    That seems evident : I test a date-time format, that can have 3 separators for the date : [- . /]
  • k102
    k102 almost 12 years
    @Oliver oh, sorry. it's the end of my working day :)
  • PiTheNumber
    PiTheNumber almost 12 years
    I added a link to the docu about delimiters.