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

时间:2017-01-23 19:23:15

标签: php wordpress plugins composer-php

作为一名图形设计师,我对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)

此代码有什么问题,以便抛出此错误消息?

3 个答案:

答案 0 :(得分:0)

您错过了.字符:

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

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

答案 1 :(得分:0)

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

  1. 问题标题中的错误:

      

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

    这是因为索引到一个在指定索引处没有偏移量的数组。

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

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

    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 (即.)来实现。

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

答案 2 :(得分:0)

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

  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,
        );