Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this PHP regex not match for accented characters?

I'm writing a quick PHP page, and I need to ignore any Strings with accented characters. I am using this preg_match() string on each word:

"[ÀÁÅÃÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ]"

(Quite a brute force method I know, but apparently [a-zA-Z] can match for accented characters)

But the function never seems to return true when it searches Strings with accented characters (Examples: "cheap…", "gustaría"...)

I haven't used Regex before, so please point out any stupid mistakes I'm making here!

like image 459
p17373 Avatar asked Oct 26 '25 02:10

p17373


1 Answers

PHP regexes need delimiters, like so:

preg_match('/[ÀÁÅÃÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ]/', "gustaría");

Note that it's also preferable to use single quotes for regex because the dollar sign could be mistaken by php as a variable.

like image 164
Daniel Vandersluis Avatar answered Oct 28 '25 16:10

Daniel Vandersluis