Truncate a UTF-8 string to fit a given byte count in PHP

Posted by fsb on Stack Overflow See other posts from Stack Overflow or by fsb
Published on 2009-12-28T00:32:37Z Indexed on 2010/04/14 17:13 UTC
Read the original article Hit count: 301

Filed under:
|

Say we have a UTF-8 string $s and we need to shorten it so it can be stored in N bytes. Blindly truncating it to N bytes could mess it up. But decoding it to find the character boundaries is a drag. Is there a tidy way?

[Edit 20100414] In addition to S.Mark’s answer: mb_strcut(), I recently found another function to do the job: grapheme_extract($s, $n, GRAPHEME_EXTR_MAXBYTES); from the intl extension. Since intl is an ICU wrapper, I have a lot of confidence in it.

© Stack Overflow or respective owner

Related posts about php

Related posts about utf-8