MadeInDreams
MadeInDreams

Reputation: 2146

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!

Upvotes: 5

Views: 5075

Answers (3)

kkyucon
kkyucon

Reputation: 46

Actually you can shorten @Casimir et Hippolyte's answer like so:

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

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

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).

Upvotes: 5

Jan
Jan

Reputation: 43199

~^([\p{L}-\s']+)$~ui

Matches the following names:

  1. Jean-François d'Abiguäel
  2. François Hollande
  3. Père Noël

See a demo on regex 101.

Upvotes: 2

Related Questions