mb_strpos vs strpos, what's the difference?

前端 未结 3 639
遥遥无期
遥遥无期 2020-12-20 12:54

Yes: I know. We should use mb_* function when we\'re working with multibyte char. But when we\'re using strpos? Let\'s take a look this code (saved in utf-8)



        
3条回答
  •  一整个雨季
    2020-12-20 13:35

    I don't the find above example fully transparent and some users may be confused.

    mb_string() should be used for multi-byte encoding, and what is multi-byte encoding you have explained in other questions, e.g. here.

    Recently we use mostly UTF encodings as UTF-8 in this example (also UTF-16) which is multi-byte character set, however usually we use only ASCII character sets (e.g. for English) and the result of strpos and mb_strpos is identical for them.

    The difference is visible when we use multi-byte characters, i.e. Chinese characters.

    echo mb_internal_encoding(); //UTF-8
    
    echo strpos('我在买绿茶', '在'); //3
    
    echo mb_strpos('我在买绿茶', '在'); //1
    

    So obviously it's applicable to Chinese characters, but also to emoji which some are not aware of.

    To give wider view how it works I show length of the following string with strlen() and mb_strlen() functions.

    echo strlen('我在买绿茶'); //15
    
    echo mb_strlen('我在买绿茶'); //5
    

提交回复
热议问题