UTF-8 validation in PHP without using preg_match()

后端 未结 5 684
梦如初夏
梦如初夏 2021-01-01 01:43

I need to validate some user input that is encoded in UTF-8. Many have recommended using the following code:

preg_match(\'/\\A(
     [\\x09\\x0A\\x0D\\x20-\\         


        
5条回答
  •  春和景丽
    2021-01-01 02:26

    You can always using the Multibyte String Functions:

    If you want to use it a lot and possibly change it at sometime:

    1) First set the encoding you want to use in your config file

    /* Set internal character encoding to UTF-8 */
    mb_internal_encoding("UTF-8");
    

    2) Check the String

    if(mb_check_encoding($string))
    {
        // do something
    }
    

    Or, if you don't plan on changing it, you can always just put the encoding straight into the function:

    if(mb_check_encoding($string, 'UTF-8'))
    {
        // do something
    }
    

提交回复
热议问题