tags:

views:

164

answers:

3
if (preg_match('(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)', '2010/02/14/this-is-something'))
{
  // do stuff
}

The above code works. However this one doesn't.

if (preg_match('/\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+/u', '2010/02/14/this-is-something'))
{
    // do stuff
}

Maybe someone could shed some light as to why the one below doesn't work. This is the error that is being produced:

A PHP Error was encountered

Severity: Warning

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

A: 

The modifier u is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32.

Also as nvl observed, you are using / as the delimiter and you are not escaping the / present in the regex. So you'lll have to use:

/\p{Nd}{4}\/\p{Nd}{2}\/\p{Nd}{2}\/\p{L}+/u

To avoid this escaping you can use a different set of delimiters like:

#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#

or

@\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+@

As a tip, if your delimiter is present in your regex, its better to choose a different delimiter not found in the regex. This keeps the regex clean and short.

codaddict
+1  A: 

Try this: (delimit the regex with ())

if (preg_match('#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#', '2010/02/14/this-is-something'))
{
   // do stuff
}

Edited

N 1.1
+1, missing delimiter is the cause.
codaddict
A: 

In the second regex you're using / as the regex delimiter, but you're also using it in the regex. The compiler is trying to interpret this part as a complete regex:

/\p{Nd}{4}/

It thinks the next character after the second / should be a modifier like 'u' or 'm', but it sees a backslash instead, so it throws that cryptic exception.

In the first regex you're using parentheses as regex delimiters; if you wanted to add the u modifier, you would put it after the closing paren:

'(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)u'

Although it's legal to use parentheses or other bracketing characters ({}, [], <>) as regex delimiters, it's not a good idea IMO. Most people prefer to use one of the less common punctuation characters. For example:

'~\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+~u'

'%\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+%u'

Of course, you could also escape the slashes in the regex with backslashes, but why bother?

Alan Moore