LoginSignup
3
3

More than 5 years have passed since last update.

文字の幅と文字のバイトの違い。

Posted at

結論からいうと、、

公式より

マルチバイト文字は、通常はシングルバイト文字の倍の幅となります。
mb_strwidth()

aの幅は1
あの幅は2

UTF8の場合
aは1byte
あは3byte

byte
3 2
こん 6 4
こんにちは 15 10
a 1 1
abc 3 3
abc012#$#% 10 10
こんにちabc 15 11
\n 1 1
\r\n 2 2

*byteはstrlen()、幅はmb_strwidth()で取得。

どこで問題になるのか

mb_strimwidth
気軽にこれを使おうとしてwidthの正体に悩まされて泣かされました。

ここで言う幅はbyteではなくmb_strwidth()の幅です。
これでスッキリきっかりstrimwidthできますね!

substr()とかmb_strcut()の引数$lengthはbyteです。

幅って書いてあるし、すぐ分かったやろという残念な感じになってしまいましたが。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3