preg_match with international characters and accents

后端 未结 2 1065
[愿得一人]
[愿得一人] 2020-12-09 17:29

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\

相关标签:
2条回答
  • 2020-12-09 18:08

    Add the UTF-8 modifier flag (u) to your expression:

    /^\p{L}+$/ui
    

    There is also no need to wrap \p{L} inside of a character class.

    0 讨论(0)
  • 2020-12-09 18:18

    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.

    0 讨论(0)
提交回复
热议问题