Search code examples
phppreg-match

Using preg_match to match weird characters


I'm trying to use preg_match to match all letters and numbers, but I've come across a weird letter that preg_match doesn't seem to match. How can I make it match? And what is that strange 'e' called? Are there more characters like that weird 'e' that I have to look out for?

<?php

$string = "pokémon";
preg_match("~(\w+)~", $string, $match);
print_r($match);

?>

Result:

Array ( [0] => pok [1] => pok )

Need Result:

Array ( [0] => pokémon [1] => pokémon )


Solution

  • You can use 'u' modifier (utf-8):

    ~(\w+)~u
    

    regex101 demo