Measure string size in Bytes in php

前端 未结 5 375
感动是毒
感动是毒 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 05:00

    Do you mean byte size or string length?

    Byte size is measured with strlen(), whereas string length is queried using mb_strlen(). You can use substr() to trim a string to X bytes (note that this will break the string if it has a multi-byte encoding - as pointed out by Darhazer in the comments) and mb_substr() to trim it to X characters in the encoding of the string.

提交回复
热议问题