Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain this UTF-8 detection regex

Tags:

regex

php

This question asked how to detect UTF-8 strings - How to detect if have to apply utf8 decode or encode on a string?

The solution was this:

if (preg_match('!!u', $string))
{
   // this is utf-8
}
else 
{
   // definitely not utf-8
}

I haven't been able to figure out how to breakdown the "!!u" expression. I clicked through all of PHP's PCRE stuff and might have missed the description for "!" marks and "u"-somethings. I tried running it through perl's YAPE::Regex::Explain (as seen in Please explain this Perl regular expression) and couldn't get something that made sense [I'm no perl expert - don't know if I fed it the right expression/string].

So... how exactly does preg_match('!!u', $string) work?

like image 582
starlocke Avatar asked Jun 01 '12 18:06

starlocke


2 Answers

It's just an empty regular expression. ! is the delimiter and u is the modfier.

As for why it works, from PHP Manual's description of the u modifier (emphasis mine):

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

like image 57
jnrbsn Avatar answered Oct 30 '22 23:10

jnrbsn


The ! is being used as the delimiter instead of /. I'll rewrite this for you, //u is the same thing. The u is a modifier that treats the pattern as utf8.

like image 28
Jonathan Kuhn Avatar answered Oct 31 '22 01:10

Jonathan Kuhn