PHPの勉強をしています。

以下のマルチバイト文字列関数がうまく動きません。
---------------------------
<?php
$str='WINGSプロジェクト';
print(mb_substr($str,5,6,'EUC-JP'));
?>
----------------------------
php.iniファイルには、extension=php_mbstring.dll
との記述があります。
php_mbstring.dllはC\php\extにあって、php.iniにはextension_dir = "c:\php\ext"との
記述があります。
何故マルチバイト文字列関数がうまく動作しないのでしょうか?よろしくお願いいたします。

回答の条件
  • 1人10回まで
  • 登録:
  • 終了:2011/08/23 12:46:51
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:Jupiter2100 No.1

回答回数444ベストアンサー獲得回数74

ポイント50pt

次のように文字化けを防止するヘッダを1行加え、ファイルをEUC-JP形式で保存してから実行してみてください。

<?php
$str='WINGSプロジェクト';
header('Content-Type: text/html; charset=EUC-JP');
print(mb_substr($str,5,6,'EUC-JP'));
?>

これでも問題が発生するようでしたら、エラーメッセージをお知らせください。

その他の回答1件)

id:Jupiter2100 No.1

回答回数444ベストアンサー獲得回数74ここでベストアンサー

ポイント50pt

次のように文字化けを防止するヘッダを1行加え、ファイルをEUC-JP形式で保存してから実行してみてください。

<?php
$str='WINGSプロジェクト';
header('Content-Type: text/html; charset=EUC-JP');
print(mb_substr($str,5,6,'EUC-JP'));
?>

これでも問題が発生するようでしたら、エラーメッセージをお知らせください。

id:Cherenkov No.2

回答回数1504ベストアンサー獲得回数493

ポイント50pt

まず確実に文字コードEUCで保存されているか確認。


<?php
echo mb_internal_encoding();

こうするとなにが出力されますか?


これでどうですか?

<?php
mb_internal_encoding('EUC-JP');
$str='WINGSプロジェクト';
header('Content-Type: text/html; charset=EUC-JP');
print(mb_substr($str,5,6,'EUC-JP'));

id:inajyun

レスありがとうございます。

両方のファイルを試しましたが、ブラウザに表示できませんでした。

2011/08/21 17:18:55
  • id:inajyun
    早速のレスありがとうございます。
    一行加えてみましたが、phpファイルをWebブラウザで見たところ何も表示されません。
    エラーメッセージもありません。
    私は、翔泳社の「独習PHP」という本でテキストエディタで書き込んでApacheでWebサーバを作って、htdocsの中に環境を作っています。
    エラーメッセージとか分かりやすいデバッグ環境とかあれば良ければ教えてください。
  • id:Jupiter2100
    次のPHPスクリプトを実行すると、PHPの設定状態が表示されます。
    実行してみてもらえますか。
    -----------------------
    <?php
    phpinfo();
    ?>
  • id:inajyun
    実行結果です。
    System Windows NT CLIENT10 6.1 build 7601 ((null) Service Pack 1) i586
    Build Date Nov 19 2009 09:58:49
    Compiler MSVC9 (Visual C++ 2008)
    Architecture x86
    Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--disable-isapi" "--with-pdo-oci=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=D:\php-sdk\oracle\instantclient11\sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/"

    Server API Apache 2.0 Handler
    Virtual Directory Support enabled
    Configuration File (php.ini) Path C:\Windows
    Loaded Configuration File C:\PHP\php.ini
    Scan this dir for additional .ini files (none)
    Additional .ini files parsed (none)
    PHP API 20090626
    PHP Extension 20090626
    Zend Extension 220090626
    Zend Extension Build API220090626,TS,VC9
    PHP Extension Build API20090626,TS,VC9
    Debug Build no
    Thread Safety enabled
    Zend Memory Manager enabled
    Zend Multibyte Support disabled
    IPv6 Support enabled
    Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar
    Registered Stream Socket Transports tcp, udp
    Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*
  • id:Jupiter2100
    php.iniが正しく配置されていないようです。
    php.iniを C:\Windows にコピーしましたか?
    ご確認ください。

    php.iniをコピーした後、Apacheを再起動してください。
  • id:inajyun
    早速のレスありがとうございます。
    C:\Windowsのphp.iniはコピーしていました。ただ、phpinfoの情報を見ると違いましたね。
    もう一度、phpinfoの情報です。
    -------------------------------------------------------------
    System Windows NT CLIENT10 6.1 build 7601 ((null) Service Pack 1) i586
    Build Date Nov 19 2009 09:58:49
    Compiler MSVC9 (Visual C++ 2008)
    Architecture x86
    Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--disable-isapi" "--with-pdo-oci=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=D:\php-sdk\oracle\instantclient11\sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/"
    Server API Apache 2.0 Handler
    Virtual Directory Support enabled
    Configuration File (php.ini) Path C:\Windows
    Loaded Configuration File C:\Windows\php.ini
    Scan this dir for additional .ini files (none)
    Additional .ini files parsed (none)
    PHP API 20090626
    PHP Extension 20090626
    Zend Extension 220090626
    Zend Extension Build API220090626,TS,VC9
    PHP Extension Build API20090626,TS,VC9
    Debug Build no
    Thread Safety enabled
    Zend Memory Manager enabled
    Zend Multibyte Support disabled
    IPv6 Support enabled
    Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar
    Registered Stream Socket Transports tcp, udp
    Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*
    ---------------------------------------------------
    で、問題の最初のマルチバイト文字列関数を使ったPHPからはWebブラウザに何も表示されません。
  • id:Jupiter2100
    1.たとえば以下のスクリプトは正常に表示されますか?
    <?php
    $str='WINGSプロジェクト';
    print($str);
    ?>

    2.ブラウザからphpinfoを見たときに、mb_系関数が利用できるようになっていれば、mb_という項目が表示されているはずです。ご確認ください。
  • id:inajyun
    レスありがとうございます。
    これは、きちんと、WINGSプロジェクト、と表示されました。

    phpinfoの内容を確認しましたが、mb_という項目はありませんでした。
  • id:Cherenkov
    http://y-kit.jp/saba/xp/phpsetup.htm
    この辺りを参考にして設定を見直す

    独習PHPを窓から投げる

    xamppでセットアップしなおす
  • id:Jupiter2100
    >phpinfoの内容を確認しましたが、mb_という項目はありませんでした。
    たぶん php_mbstring.dll がロードされていません。

    "c:\php\ext\php_mbstring.dll" が存在しているかどうか確認してください。
  • id:inajyun
    レスありがとうございます。
    それが私も気付いて、そのdllを確認していたのですが、御指摘のパスにきちんとあるのです。
    それなのにロードされていません。
  • id:Jupiter2100
    PCを再起動してみてください。
    それでうまく動かないようでしたら、Apache, PHPを再インストールした方がいいと思います。
  • id:inajyun
    レスありがとうございます。
    Apache、PHPは再インストールしました。
    ですが、Apacheは2.2.14、PHPは5.3.6です。
    ですが、phpinfoを表示させるとPHPのバージョンが5.3.1となってしまいます。
    また、phpinfoのdateの部分は、
    Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for '9.0/no DST' instead in D:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\samples\chap04\hello.php on line 1
    とtimezoneのエラーが出ます。C:\windows\php.iniのtimezoneを'Asia/Tokyo'にしても駄目です。

    それと、ジュピターさんの最初のコードを実行すると、

    Fatal error: Call to undefined function mb_substr() in D:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\samples\chap04\substr.php on line 4

    と出てきます。
  • id:inajyun
    追記です。
    PHP5.3.6のモジュールの雛形から作ったphp.iniをC:\Windowsに置いたら、phpinfo()のエラーが出なくなりました。
    最初に問題にしていた、マルチバイト文字の切り取りの処理もうまくいきました。
    ありがとうございます。

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません