preg_match with international characters and accents

Viewed 15676

I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok". I don't know what I am doing wrong, can you help? thanks

$string = 'é';

if(preg_match('/^[\p{L}]+$/i', $string)){

    echo 'ok';

} else{

    echo 'nok';
}
2 Answers

I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.

<?php
$string = 'États unis and états unis';

if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){

echo 'ok';

} else{

echo 'nok';
}

?>

If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9

Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:

if (trim($_POST['forminput']) == "") {... some error message ...}

to reject the submission.

Related