preg_match:如果有html实体怎么办?

I got this function:

preg_match("/öppet/iu", $string);

What if the "ö" is written as a html entity? Like this: ö or Ö Is there a way to find them as well?

Sure, just decode HTML entities before matching:

preg_match("/öppet/iu", html_entity_decode($string));

This has the benefit of working for other characters as well, e.g. if you want to match ä too at some point in the future.

You could do:

preg_match("/(?:ö|ö)ppet/iu", $string);

The ?: prevents the match from being captured to save memory, the | is "or"

If you are using a PHP version below 5.4.0 you have to specify the use of the UTF-8 encoding:

preg_match("/öppet/iu", html_entity_decode($string, ENT_COMPAT, 'UTF-8'));

Hope this will help.