目的
PHPでmb_language()やmb_internal_encoding()を使おうとしたら、エラー「Fatal error: Uncaught Error: Call to undefined function mb_language()」が出たので、解決する。
環境
# vagrant
$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
$ php -v
PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
$ httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 8 2019 11:41:18
(*最後の方で、ec2のAmazon Linux 2の場合についても少し書いています。)
php-mbstringをインストールする
解決策として、必要なモジュールphp-mbstringをインストールします。
$ sudo yum install php-mbstring
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* remi-safe: ftp.riken.jp
* updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ php-mbstring.x86_64 0:5.4.16-48.el7 を インストール
--> 依存性の処理をしています: php-common(x86-64) = 5.4.16-48.el7 のパッケージ: php-mbstring-5.4.16-48.el7.x86_64
--> 依存性解決を終了しました。
エラー: パッケージ: php-mbstring-5.4.16-48.el7.x86_64 (base)
要求: php-common(x86-64) = 5.4.16-48.el7
インストール: php-common-7.4.3-1.el7.remi.x86_64 (@remi-php74)
php-common(x86-64) = 7.4.3-1.el7.remi
利用可能: php-common-5.4.16-48.el7.x86_64 (base)
php-common(x86-64) = 5.4.16-48.el7
問題を回避するために --skip-broken を用いることができます。
これらを試行できます: rpm -Va --nofiles --nodigest
エラーが出て、失敗しました。
調べたら、php7をremiでインストールした場合、リポジトリにremiリポジトリを指定する必要があるそうです。
ですので、リポジトリを指定して、再度インストールします。
$ sudo yum install --enablerepo=remi-php74 php-mbstring
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* remi-php74: ftp.riken.jp
* remi-safe: ftp.riken.jp
* updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ php-mbstring.x86_64 0:7.4.16-1.el7.remi を インストール
〜
〜
インストール:
php-mbstring.x86_64 0:7.4.16-1.el7.remi
依存性関連をインストールしました:
oniguruma5php.x86_64 0:6.9.6-1.el7.remi
依存性を更新しました:
php.x86_64 0:7.4.16-1.el7.remi php-cli.x86_64 0:7.4.16-1.el7.remi php-common.x86_64 0:7.4.16-1.el7.remi php-json.x86_64 0:7.4.16-1.el7.remi
php-sodium.x86_64 0:7.4.16-1.el7.remi
完了しました!
今度はインストールできました!
yum list installedでphp-mbstringが追加されているのがわかります。
$ yum list installed | grep php
oniguruma5php.x86_64 6.9.6-1.el7.remi @remi-safe
php.x86_64 7.4.16-1.el7.remi @remi-php74
php-cli.x86_64 7.4.16-1.el7.remi @remi-php74
php-common.x86_64 7.4.16-1.el7.remi @remi-php74
php-json.x86_64 7.4.16-1.el7.remi @remi-php74
php-mbstring.x86_64 7.4.16-1.el7.remi @remi-php74
php-sodium.x86_64 7.4.16-1.el7.remi @remi-php74
設定を反映させるために、Apacheを再起動します。
$ sudo systemctl restart httpd
これで、php-mbstringが使えるようになって、mb_language()やmb_internal_encoding()も問題なく使用できるようになりました。
また、phpinfo()の出力を見ても、下の画像のようにmbstringが追加されていました。
ec2のAmazon Linux 2の場合
上で書いたのはCentOS7の場合ですが、ec2のAmazon Linux 2の場合は、yumでphp-mbstringをインストールする時、以下のようにphp74-php-mbstringをインストールする必要がありました(PHPのバージョンは7.4)。
$ sudo yum install php74-php-mbstring
php74-php-mbstringインストール後の、yum list installedの結果は以下になります。
$ yum list installed | grep php
oniguruma5php.x86_64 6.9.6-1.el7.remi @remi-safe
php-common.x86_64 5.4.16-46.amzn2.0.2 @amzn2-core
php74.x86_64 1.0-3.el7.remi @remi-safe
php74-php.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-cli.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-common.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-fpm.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-json.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-mbstring.x86_64 7.4.16-1.el7.remi @remi-safe
php74-php-sodium.x86_64 7.4.16-1.el7.remi @remi-safe
php74-runtime.x86_64 1.0-3.el7.remi @remi-safe
参考サイト
phpの「Fatal error: Call to undefined function mb_language() in …」を解決する方法
CentOSにインストールしたnginxでPHPを動かしてみる。