0

I have at the moment a problem on a Symfony project, I have to create people account from a form, but I have a problem, I want to avoid every data that are not real firstname/lastname.

So tried to do it with a Regex constraint but since I'm bad at it, I can't made it work like I want. I want only letters but allow accentuated letters (like 'é' or 'è)and also œ,' or -.

And tried something like this but it doesn't work : #^([a-z]+(( |')[a-z]+))+([-]([a-z]+(( |')[a-z]+))+)*$#iu

If someone can help me, I would really appreciate it :)

Esca
  • 33
  • 3

3 Answers3

0

I think you should better only check that it is not empty. If you really wish to use a regex, this may work :

/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
0

You can use this code:

$name = "Á é í ó ú aœa'-ax"; //use trim() if it comes from a form
$regex = "/^[A-Za-zÀ-úœ'\-\s]+$/";

preg_match($regex, $name, $matches);

print_r($matches);

Output:

Array ( [0] => Á é í ó ú aœa'-ax )

You can manually add more special chars as you wish. Here is a good list for your understanding: https://unicode-table.com/en/#latin-1-supplement

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
0

Use unicode properties for any letter in any language \p{L} combine with any accent \p{Mn} or hyphen \p{Pd}

/^[\p{L}\p{Mn}\p{Pd}]+(?:\s[\p{L}\p{Mn}\p{Pd}]+)*$/u

and don't forget: https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

Toto
  • 89,455
  • 62
  • 89
  • 125