douxing9228 2019-01-02 21:28
浏览 138
已采纳

php 7.2升级错误“无法为字符串偏移量分配空字符串”

I'm working with an aged wordpress theme which I really like and I only have some basic coding skills. My provider forcably upgraded my server php version to 7.2 and of course some of my scripts are breaking down.

public function localize( $handle, $object_name, $l10n ) {
    if ( $handle === 'jquery' )
        $handle = 'jquery-core';

    if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
        $after = $l10n['l10n_print_after'];
        unset($l10n['l10n_print_after']);
    }

    foreach ( (array) $l10n as $key => $value ) {
        if ( !is_scalar($value) )
            continue;

        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
    }``

According to the log the error is in the last line because in that like apparently it "Cannot assign an empty string to a string offset"

Maybe this is a lot more complicated than changing one simple thing....any solutions for that?

  • 写回答

2条回答 默认 最新

  • douliang2087 2019-01-02 21:38
    关注

    The most logical thing to do here is to change it to update in the in_array condition:

    if ( is_array($l10n){
        if(isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
            $after = $l10n['l10n_print_after'];
             unset($l10n['l10n_print_after']);
        }
        foreach ($l10n as $key => $value ) {
            if ( !is_scalar($value) )
                continue;
    
            $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
        }
    }
    

    Even if you cast (array)$l10n to an array, this doesn't set the variable itself to be an array ( like $l10n = (array)$l10n).

    That said, working with mixed types can be really cumbersome. It's better to send it only arrays or deal with the array bit first, that way you have a consistent type. Like this:

    public function localize( $handle, $object_name, $l10n ) {
        //normalize arguments
        if(!is_array($l10n)) $l10n = [$l10n];
    
        if ( $handle === 'jquery' )
            $handle = 'jquery-core';
    
        if ( isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
            $after = $l10n['l10n_print_after'];
            unset($l10n['l10n_print_after']);
        }
    
        foreach ($l10n as $key => $value ) {
            if ( !is_scalar($value) )
                continue;
    
            $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。