Measure string size in Bytes in php

前端 未结 5 379
感动是毒
感动是毒 2020-12-01 04:11

I am doing a real estate feed for a portal and it is telling me the max length of a string should be 20,000 bytes (20kb), but I have never run across this before.

Ho

5条回答
  •  一个人的身影
    2020-12-01 04:58

    You can use mb_strlen() to get the byte length using a encoding that only have byte-characters, without worring about multibyte or singlebyte strings. For example, as drake127 saids in a comment of mb_strlen, you can use '8bit' encoding:

    
    

    You can have problems using strlen function since php have an option to overload strlen to actually call mb_strlen. See more info about it in http://php.net/manual/en/mbstring.overload.php

    For trim the string by byte length without split in middle of a multibyte character you can use:

    mb_strcut(string $str, int $start [, int $length [, string $encoding ]] )
    

提交回复
热议问题