p17373
p17373

Reputation: 43

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!

Upvotes: 4

Views: 2024

Answers (1)

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94153

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.

Upvotes: 4

Related Questions