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条)

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题