• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

PHPを勉強中なわけですが、下記のようなスクリプトを書いたところ、mb_strlen という関数が使えませんでした。

   1  <?php
   2  
   3  echo "テキストを入力:";
   4  $a = trim(fgets(STDIN));
   5  echo mb_strlen($a, 'utf-8') . "文字あります。";
   6  
   7  ?>

   1  $ php hello.php
   2  テキストを入力:hello
   3  PHP Fatal error:  Call to undefined function mb_strlen() in /home/akanuma/scripts/php/hello.php on line 5

ググってみるとどうやらマルチバイトの処理ができるようにするには、コンパイル時にパラメータ追加したりとか、php.ini に設定の追加が必要とか色々ありましたが、yum で php-mbstring をインストールすればよさそうということで実行。

   1  # yum install php-mbstring
   2  Loaded plugins: fastestmirror
   3  Loading mirror speeds from cached hostfile
   4   * base: ftp.iij.ad.jp
   5   * epel: ftp.kddilabs.jp
   6   * extras: ftp.iij.ad.jp
   7   * remi: rpms.famillecollet.com
   8   * updates: ftp.iij.ad.jp
   9  Setting up Install Process
  10  Resolving Dependencies
  11  --> Running transaction check
  12  ---> Package php-mbstring.x86_64 0:5.3.14-1.el5.remi set to be updated
  13  --> Finished Dependency Resolution
  14  
  15  Dependencies Resolved
  16  
  17  ========================================================================================================================
  18   Package                       Arch                    Version                              Repository             Size
  19  ========================================================================================================================
  20  Installing:
  21   php-mbstring                  x86_64                  5.3.14-1.el5.remi                    remi                  2.3 M
  22  
  23  Transaction Summary
  24  ========================================================================================================================
  25  Install       1 Package(s)
  26  Upgrade       0 Package(s)
  27  
  28  Total download size: 2.3 M
  29  Is this ok [y/N]: y
  30  Downloading Packages:
  31  php-mbstring-5.3.14-1.el5.remi.x86_64.rpm                                                        | 2.3 MB     00:04
  32  Running rpm_check_debug
  33  Running Transaction Test
  34  Finished Transaction Test
  35  Transaction Test Succeeded
  36  Running Transaction
  37    Installing     : php-mbstring                                                                                     1/1
  38  
  39  Installed:
  40    php-mbstring.x86_64 0:5.3.14-1.el5.remi
  41  
  42  Complete!

これで無事に mb_strlen が使えるようになりました。

   1  $ php hello.php
   2  テキストを入力:はろー
   3  3文字あります。

posted by Png akanuma on Sat 14 Jul 2012 at 18:48
Contents
[PHP] yum で php-mbstring をインストール
Comments
yoku0825: nkfの方が使いやすいですが、glibcに含まれているiconvの方が 確実にどの環境にも入っ... '12-6
Hiroaki Akanuma: ご指摘ありがとうございます。「嗜好度は大きい値がより嗜好度が強いことを意味すればどのような値で... '11-11
akr: 初めての推薦エンジンのところ 「属性データはどんなものでも可能。」 は preference ... '11-11
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ