2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP strlen() と mb_strlen()違い & substrとmb_substr違い

Last updated at Posted at 2021-03-11

#文字のバイトによる長さか文字の数か

コード


echo strlen('アイウエオ').PHP_EOL; //15
echo strlen('ア').PHP_EOL;//3
echo strlen('a').PHP_EOL;//1
echo strlen('あ').PHP_EOL;//3

echo mb_strlen('アイウエオ').PHP_EOL;//5
echo mb_strlen('ア').PHP_EOL;//1
echo mb_strlen('a').PHP_EOL;//1
echo mb_strlen('あ').PHP_EOL;//1

echo substr('アイウエオ',0,1).PHP_EOL;//�
echo substr('ア',0,1).PHP_EOL;//�
echo substr('a',0,1).PHP_EOL;//a
echo substr('あ',0,3).PHP_EOL;//あ

echo mb_substr('アイウエオ',0,1).PHP_EOL;//ア
echo mb_substr('ア',0,1).PHP_EOL;//ア
echo mb_substr('a',0,1).PHP_EOL;//a
echo mb_substr('あ',0,3).PHP_EOL;//あ

substrとmb_substrは第一引数で指定した文字列の第二引数で指定した位置から第三引数で指定したバイト数(mb_substrでは文字数)分の文字を出力する。
substrでは第三引数を1とするとバイトが3の日本語はエラーになる

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?