1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP: undefined function mb_substr()

Last updated at Posted at 2022-03-18

次のようなエラーがでた時の対策

PHP Fatal error:  Uncaught Error: Call to undefined function mb_substr()

このエラーは、/var/log/nginx/error.log に出ました。

mbstring がインストールされているかの確認

php -m | grep mbstring

Ubuntu でのインストール方法

sudo apt install php-mbstring

インストールされたことの確認

$ php -m | grep mbstring
mbstring

サーバーの再起動

sudo systemctl restart php8.4-fpm
sudo systemctl restart nginx

次のバージョンで確認しました。

$ php --version
PHP 8.4.5 (cli) (built: Jul 14 2025 18:20:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.5, Copyright (c), by Zend Technologies

テストに使った PHP プログラム

test_a005.php
<?php
echo "<html>";
echo '<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />';
echo "<body>";
echo "*** start *** <p />";
echo "test_a005.php<p />";
echo "<h2>テスト</h2>";
//
// mb_internal_encoding("UTF-8");

$text = "こんにちは、PHPの世界へようこそ!";

// 文字列の切り出し
$substring = mb_substr($text, 0, 5); // 最初の5文字を取得
echo $substring; // 出力: "こんにちは"

// 負の値を使用して末尾から切り出す
$substring = mb_substr($text, -7); // 最後の7文字を取得
echo $substring; // 出力: "へようこそ!"

// 途中から切り出す
$substring = mb_substr($text, 6, 4); // 6文字目から4文字取得
echo $substring; // 出力: "、PHP"
//
echo "*** end *** <p />";
echo "</body>";
echo "</html>";
?>
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?