简体   繁体   English

调试iconv_strlen错误 - PHP 5.5

[英]Debug iconv_strlen error - PHP 5.5

I am trying to export a document to PDF using Laravel & DomPDF. 我正在尝试使用Laravel和DomPDF将文档导出为PDF。 This works on my mac, but not on staging or live server. 这适用于我的Mac,但不适用于登台或实时服务器。 Error as follows: 错误如下:

I have no idea what this means, and cannot find any solutions. 我不知道这意味着什么,也找不到任何解决方案。

iconv_strlen(): Wrong charset, conversion from 8bit//IGNORE' to UCS-4LE' is not allowed iconv_strlen(): Wrong charset, conversion from UCS-4LE' is not allowed iconv_strlen(): Wrong charset, conversion from 8位// IGNORE' iconv_strlen(): Wrong charset, conversion fromUCS-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. 如果您需要任何进一步的信息来帮助我进行调试,请在评论中提问。

Problem solved. 问题解决了。 Thanks BrianS. 谢谢BrianS。

This was solved by re-installing mbstring. 这是通过重新安装mbstring解决的。

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

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 Cpdf class contains about 30 calls to mb_strlen() with the $encoding parameter set to '8bit' , which is a valid encoding for mb_strlen() . 最新版本CpdfCpdf类包含对mb_strlen()大约30个调用,其中$encoding参数设置为'8bit' ,这是mb_strlen()有效编码

Laravel's composer.json requires patchwork/utf8 . Laravel的composer.json需要patchwork/utf8 It provides the mb_strlen() shim which calls iconv_strlen() . 它提供了调用iconv_strlen()mb_strlen() shim。

PHP usually uses either glibc or libiconv for its iconv module. PHP通常使用glibclibiconv作为其iconv模块。 For both libraries, the list of supported encodings can be displayed using iconv --list . 对于这两个库,可以使用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 mbstring PHP module causes mb_strlen() to be executed natively, so the shim isn't used and the error doesn't occur. 安装mbstring PHP模块会导致mb_strlen()本机执行,因此不使用填充程序并且不会发生错误。

Update 更新

@rofavadeka One solution is to create a fork of the dompdf repo, and replace every use of '8bit' encoding with a different 8-bit encoding which is supported by mb_strlen() , glibc and libiconv . @rofavadeka一种解决方案是创建一个的的DOMPDF回购,并取代在每次使用的'8bit'与由支持不同的8位的编码方式进行编码mb_strlen() glibclibiconv

I've written a script to determine which encodings are suitable. 我写了一个脚本来确定哪些编码是合适的。 Here's the output of the script for glibc and libiconv . 这是glibclibiconv脚本的输出。 The suitable encodings are: 合适的编码是:

I was getting that error in Hash:make() during seeding my DB for testing. 我在Hash:make()遇到了这个错误Hash:make()在播放我的数据库进行测试时。

Enabling php_mbstring in php-cli.ini caused it. php-cli.ini启用php_mbstring php-cli.ini导致它。

In Windows the solution is: remove the semicolon before 在Windows中,解决方案是:之前删除分号

 extension=php_mbstring.dll

If you are using WHM then you can use EasyApache to rebuild. 如果您使用WHM,则可以使用EasyApache进行重建。 Once you get the modules options after selecting your version of PHP select the "Exhaustive Options List" button. 在选择PHP版本后获得模块选项后,选择“穷举选项列表”按钮。 Then ctrl+f "mbstring" and it should come up. 然后ctrl + f“mbstring”它应该出现。 Mark the check box and rebuild. 选中复选框并重建。 It should work. 它应该工作。

If you are on wamp or some custom stack, remember that the php in your may be using a custom php_something.ini for the apache use, due to which even if the GUI is showing that mbstring is on (uncommented) still its quiet possible that that same line is commented out(disabled/ off ) inside the actual php.ini file ( True Story ). 如果你在wamp或一些自定义堆栈,请记住你的中的php可能正在使用自定义php_something.ini进行apache使用,因此即使 GUI显示mbstring打开 (取消注释)仍然安静可能在实际的php.ini文件( True Story )中注释掉(禁用/ 关闭 )相同的行。

Solution :- Just navigate to 解决方案 : - 导航到

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

In this directory you shall find various .ini files named slightly differently, Mine was using php_uwamp.ini for the stack but for the CLI , php was 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. 这让我长时间拉毛,以为它可能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM