I'm trying to validate a string that must conform to the following rules:
This is how far I got:
if (preg_match("/^[\p{L}\p{M}][\s\p{L}\p{M}-'\.]{4,}$/u", $name, $matches)) {
echo "Match was found: '{$matches[0]}' in '$name'<br />";
}
I'm having difficulty writing the min 4 alphanumeric characters with a space in it condition.
I'm tying to match an entity's full name, but with some relaxed conditions.
:
"ábc é" --> good
"á bcd" --> good
"abc déf" --> good
"ab cd éf" --> good
"a-1 b4." --> good
"a 123--" --> good
"a 12'34 .-56" --> good
"á" --> bad less than 4 alphanumeric
"ab" --> bad less than 4 alphanumeric
"ábc" --> bad less than 4 alphanumeric
"abcd" --> bad no white space in the string
"1ábc d" --> bad starts with a non letter
"-ábc d" --> bad starts with a non letter
".1ábc d" --> bad starts with a non letter
This might work, but havn't tested it.
Edit: Well, after testing/debugging, this is what I get, good luck!
# ^(?=[\pL\pN\s'\-.]+$)(?=[^\pL\pN]*(?:[\pL\pN][^\pL\pN]*){4,}$)(?![\pN'\-.])(?=\S+\s+\S)
^
(?= [\pL\pN\s'\-.]+ $ ) # Allowed characters
(?= # At least 4 alphanumeric chars
[^\pL\pN]*
(?: [\pL\pN] [^\pL\pN]* ){4,}
$
)
(?! [\pN'\-.] ) # Cannot start with these
(?= # At least 1 whitespace after first char / before last char
\S+ \s+ \S
)