【问题标题】:Warning: Illegal string offset ‘content’警告:非法字符串偏移“内容”
【发布时间】:2017-06-08 09:59:36
【问题描述】:

作为一名图形设计师,我对 PHP 代码不太了解。

警告:第 908 行的 webaddress/class-vc-frontend-editor.php 中存在非法字符串偏移“内容”

我正在使用 Visual Composer 的前端编辑器,并且在检查代码时不断出现此错误,我得到以下信息:

 );
        if ( false !== WPBMap::getParam( $s, 'content' ) ) {
            $shortcode['attrs']['content'] = $s 'content';
        }

我精通 HTML、CSS 和 Scripter,但我希望有高级 PHP 技能的人能够回答我的问题。

如果我在 Dreamweaver 中打开文件以尝试诊断错误,我会得到以下信息:

第 908 行:语法错误,意外引用字符串 (T_CONSTANT_ENCAPSED_STRING)

这段代码有什么问题导致它抛出这个错误消息?

【问题讨论】:

    标签: php wordpress plugins composer-php


    【解决方案1】:

    对于来这里问这个问题的人:

    1. 正确的行应该是$shortcode['attrs']['content'] = $content;

    2. 错误是由于 VC 没有检索到正则表达式中的内容,前几行:

      $shortcode = array(
              'tag' => $s,
              'attrs_query' => $found[3][ $index ],
              'attrs' => shortcode_parse_atts( $found[3][ $index ] ), //here
              'id' => $id,
              'parent_id' => $parent_id,
          );
      

    【讨论】:

      【解决方案2】:

      此代码似乎至少有两个错误。有关说明,请参阅下面的列表项。

      1. 问题标题中的错误:

        警告:非法字符串偏移“内容”

        这来自于对指定索引处没有偏移量的数组的索引。

        $shortcode['attrs']['content'] = $s 'content';
        

        如果$shortcode['attrs'] 不是数组或索引'content' 处没有元素,则会发生此错误。您没有解释 $shortcode 的来源,但避免此错误的一种方法是使用 array_key_exists() 检查数组是否在 content 处具有索引:

        if (array_key_exists('content', $shortcode['attrs'])) {
            $shortcode['attrs']['content'] = $s 'content';
        }
        else {
            //handle case where index isn't defined
        }
        

        为了更安全,最好确保它是一个带有is_array() 的数组:

        if (is_array($shortcode['attrs']) && array_key_exists('content', $shortcode['attrs'])) {
        
      2. 你提到的第二个错误:

        第 908 行:语法错误,意外引用字符串 (T_CONSTANT_ENCAPSED_STRING)

        这是因为在赋值的右侧(在同一行中)变量$s 位于字符串文字(即'content')之前,两者之间没有运算符。我不知道您到底想对这两个操作数做什么,但也许应该将它们连接起来。这可以通过string operator dot(即.)来实现。

        $shortcode['attrs']['content'] = $s . 'content';
        

      【讨论】:

        【解决方案3】:

        您错过了. 字符:

        $shortcode['attrs']['content'] = $s.'content';
        

        这里是关于字符串运算符的manual。

        【讨论】:

        • 感谢您的帮助,非常感谢,希望我能在某个时候回报您的帮助:D
        猜你喜欢
        • 2021-09-30
        • 2013-09-13
        • 2013-04-11
        • 2012-04-09
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多