Q:

Best preg_match expression for international name

I'm trying to find a good preg_match expression that allow all letters (included accented letters), the space , and these symbols: - '
I thought this could be a good solution '/^\p{L}+$/ui', but I can't find a successful way to add the symbols I need, if that is possible...
Otherwise I guess I should use something like this:
'#^[a-zA-Z0-9àòäöüÄÖÜ \.\]]+$#'

but there are so many accents that I was hoping to find a better solution.

A:

Try placing the \p{L} and allowed symbols inside brackets:
$name = "B-jör n'Bòrg";
if (preg_match("/^[- '\p{L}]+$/u", $name)) {
    echo "$name is a valid name!"; // It is
}

You may also want to add some additional checks, e.g. to make sure that names starts and ends with a letter and not a symbol.
Edit
This will make sure that names starts/ends with a letter and does not contain consecutive symbols:
$name = "-Björ n''Bòrg-";
if (preg_match("/^\p{L}([- ']\p{L}|\p{L})*$/u", $name)) {
    echo "$name is a valid name!"; // It's not
}

