View Single Post

  #4 (permalink)  
Old 09-06-2004
Chung Leong
 
Posts: n/a
Default Re: Problem with length of Multibyte String

"lian" <lian@fed.com> wrote in message news:2q04g4Fokjp2U1@uni-berlin.de...
> Hi all,
> I want to write some UTF-8 Chinese characters to file with following
> php codes:
>
> <code>
> .......
> $fp = fopen($filepath,'wb');
> fwrite($fp,$utf8string,strlen($utf8string));
> fclose($fp);
> ........
> </code>
>
> Problem happened on function "strlen". utf-8 string consists of
> multibye characters. Every characters have more than one byte. But to
> function "strlen", every character is just one byte.
> Take Chinese character "?" for example, its utf-8 code is "0xE6
> 0x88 0x91",obviously 3 bytes, but strlen return 1 byte. And then
> function "fwrite" just write 1 byte to the file.
> So I wonder if there are any way to get actural length of multibyte
> string in PHP?


First you all, you don't need to pass the length to fwrite() if you want to
whole string written. Just fwrite($fp, $utf8string) will do.

Second, your description of strlen() is wrong. It returns the byte count,
never the Unicode character count.


Reply With Quote