laravel 调试 iconv_strlen 错误 - PHP 5.5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22492776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:13:40  来源:igfitidea点击:

Debug iconv_strlen error - PHP 5.5

phplaraveldompdficonvmbstring

提问by Gravy

I am trying to export a document to PDF using Laravel & DomPDF. This works on my mac, but not on staging or live server. Error as follows:

我正在尝试使用 Laravel 和 DomPDF 将文档导出为 PDF。这适用于我的 Mac,但不适用于登台或实时服务器。错误如下:

I have no idea what this means, and cannot find any solutions.

我不知道这意味着什么,也找不到任何解决方案。

iconv_strlen(): Wrong charset, conversion from8bit//IGNORE' to UCS-4LE' is not allowed

iconv_strlen(): Wrong charset, conversion from8位//忽略'到 UCS-4LE' is not allowed

open: /srv/www/html/vendor/patchwork/utf8/class/Patchwork/PHP/Shim/Mbstring.php

        return true;
    }

    static function mb_strlen($s, $encoding = INF)
    {
        INF === $encoding && $encoding = self::$internal_encoding;
        return iconv_strlen($s, $encoding . '//IGNORE');
    }

I have tried adding the following to .htaccess

我尝试将以下内容添加到 .htaccess

AddDefaultCharset UTF-8

I have tried adding the following to the top of the view which I am trying to generate the pdf for:

我尝试将以下内容添加到我试图为其生成 pdf 的视图顶部:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

If you need any further information to assist me in debugging, please ask in comments.

如果您需要任何进一步的信息来帮助我进行调试,请在评论中提问。

采纳答案by Gravy

Problem solved. Thanks BrianS.

问题解决了。谢谢布赖恩。

This was solved by re-installing mbstring.

这是通过重新安装 mbstring 解决的。

sudo yum --disablerepo="*" --enablerepo="remi*"
install php-mbstring*
sudo httpd -k restart

回答by TachyonVortex

BrianS's solution does indeed solve the issue, but I thought it'd be interesting to explain what caused the original problem.

BrianS 的解决方案确实解决了这个问题,但我认为解释导致原始问题的原因会很有趣。

In the latest release of dompdf, the Cpdfclass contains about 30 calls to mb_strlen()with the $encodingparameter set to '8bit', which is a valid encoding for mb_strlen().

DOMPDF的最新版本中,Cpdf类包含约30调用mb_strlen()$encoding参数集'8bit',这是一个 为有效编码mb_strlen()

Laravel's composer.jsonrequires patchwork/utf8. It provides the mb_strlen()shim which calls iconv_strlen().

Laravel 的composer.json需要patchwork/utf8. 它提供了mb_strlen()调用的垫片iconv_strlen()

PHP usually uses either glibcor libiconvfor its iconvmodule. For both libraries, the list of supported encodings can be displayedusing iconv --list.

PHP 通常使用glibclibiconv为其iconv模块。对于这两个库,可以支持的编码列表中 显示的使用iconv --list

Neither of those libraries support an encoding called '8bit', which is why iconv_strlen()throws that error:

这些库都不支持称为 的编码'8bit',这就是iconv_strlen()抛出该错误的原因:

Wrong charset, conversion from '8bit//IGNORE' to 'UCS-4LE' is not allowed

Installing the mbstringPHP module causes mb_strlen()to be executed natively, so the shim isn't used and the error doesn't occur.

安装mbstringPHP 模块会导致mb_strlen()本机执行,因此不使用 shim 并且不会发生错误。

Update

更新

@rofavadeka One solution is to create a forkof the dompdfrepo, and replace every use of '8bit'encoding with a different 8-bit encoding which is supported by mb_strlen(), glibcand libiconv.

@rofavadeka一种解决方案是创建一个 的的 DOMPDF回购,并取代在每次使用的 '8bit'与由支持不同的8位编码的编码 mb_strlen()glibclibiconv

I've written a scriptto determine which encodings are suitable. Here's the output of the script for glibcand libiconv. The suitable encodings are:

我已经编写了一个脚本来确定哪些编码是合适的。这是glibc和 脚本的输出 libiconv。合适的编码是:

回答by Vani Murarka

I was getting that error in Hash:make()during seeding my DB for testing.

Hash:make()为我的数据库播种进行测试时,我遇到了这个错误。

Enabling php_mbstringin php-cli.inicaused it.

启用php_mbstringphp-cli.ini造成的。

In Windows the solution is: remove the semicolon before

在 Windows 中,解决方案是:删除之前的分号

 extension=php_mbstring.dll

回答by Goddard

If you are using WHM then you can use EasyApache to rebuild. Once you get the modules options after selecting your version of PHP select the "Exhaustive Options List" button. Then ctrl+f "mbstring" and it should come up. Mark the check box and rebuild. It should work.

如果您使用 WHM,那么您可以使用 EasyApache 来重建。在选择 PHP 版本后获得模块选项后,选择“Exhaustive Options List”按钮。然后 ctrl+f "mbstring" 它应该出现。选中复选框并重建。它应该工作。

回答by Mohd Abdul Mujib

If you are on wamp or some custom stack, remember that the phpin your wampmay be using a custom php_something.inifor the apache use, due to which even if the wampGUI is showing that mbstringis on(uncommented) still its quiet possible that that same line is commented out(disabled/off) inside the actual php.inifile (True Story).

如果您使用的是 wamp 或某些自定义堆栈,请记住,php您的wamp 中可能正在使用php_something.ini用于 apache的自定义,因此即使wampGUI 显示mbstring打开(未注释)它仍然可能在同一行中保持安静在实际文件(真实故事)中被注释掉(禁用/关闭)。php.ini

Solution:- Just navigate to

解决方案:-只需导航到

wamp dir > bin > php > phpx.x.x >

In this directory you shall find various .inifiles named slightly differently, Mine was using php_uwamp.inifor the stack but for the CLI, phpwas using the adjacent file named php.ini.

在这个目录中,你会发现不同的.ini命名有少许不同,矿用文件php_uwamp.ini堆栈但对于CLIphp用名为相邻的文件php.ini

This had me pulling hairs for quiet long, thought it might help someone.

这让我很长时间安静地拉头发,认为这可能对某人有所帮助。