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/script s/php/hell o.php on line 5
ググってみるとどうやらマルチバイトの処理ができるようにするには、コンパイル時にパラメータ追加したりとか、php.ini に設定の追加が必要とか色々ありましたが、yum で php-mbstri
1 # yum install php-mbstring 2 Loaded plugins: fastestmir ror 3 Loading mirror speeds from cached hostfile 4 * base: ftp.iij.ad .jp 5 * epel: ftp.kddila bs.jp 6 * extras: ftp.iij.ad .jp 7 * remi: rpms.famil lecollet.c om 8 * updates: ftp.iij.ad .jp 9 Setting up Install Process 10 Resolving Dependenci es 11 --> Running transactio n check 12 ---> Package php-mbstri ng.x86_64 0:5.3.14-1 .el5.remi set to be updated 13 --> Finished Dependency Resolution 14 15 Dependenci es Resolved 16 17 ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== 18 Package Arch Version Repository Size 19 ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== 20 Installing : 21 php-mbstri ng x86_64 5.3.14-1.e l5.remi remi 2.3 M 22 23 Transactio n 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 Downloadin g Packages: 31 php-mbstri ng-5.3.14- 1.el5.remi .x86_64.rp m | 2.3 MB 00:04 32 Running rpm_check_ debug 33 Running Transactio n Test 34 Finished Transactio n Test 35 Transactio n Test Succeeded 36 Running Transactio n 37 Installing : php-mbstri ng 1/1 38 39 Installed: 40 php-mbstri ng.x86_64 0:5.3.14-1 .el5.remi 41 42 Complete!
これで無事に mb_strlen が使えるようになりました。
1 $ php hello.php 2 テキストを入力:はろー 3 3文字あります。
posted by
akanuma
on Sat 14 Jul 2012
at 18:48