Skip to content
Advertisement

preg_match accented characters

I have an issue using preg_match with php.

I want my users to fill the Name field with only valid characters. Ex: no numbers or special chars.

My site will eventually be bilingual but most of my visitors are french Canadians

I prefer utf-8 for my encoding. So at the top of my document i have this tag :

<meta charset="utf-8" />

I need to accept accented characters in my form and i have tryed this :

(preg_match('/^p{L}+$/ui',$string))

But i cant get accent to be accepted this way.

Here is an example of what a name could contain as characters

jean-françois d’abiguäel

That’s pretty much as bad as it could get

Everyone seems to get (preg_match('/^p{L}+$/ui',$string)) working, but me.

I would need something like this :

/^p{L}(p{L}+[- ']?)*p{L}$/ui

But i need to get it working.

My servers are IIS (godaddy) PHP Version is 5.4 default timezone is set to America/Montreal

Thank you!

Advertisement

Answer

This pattern should work:

/^pL+(?>[- ']pL+)*$/u

demo

But feel free to adapt it for more exotic names (For example names with a trailing quote or an apostrophe).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement