首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将短代码解析为数组

将短代码解析为数组
EN

Stack Overflow用户
提问于 2013-09-29 09:23:10
回答 2查看 3.2K关注 0票数 2

我需要在php中将WordPress短代码解析为数组,例如:

代码语言:javascript
复制
[parrent_shortcode attribute='1'  attribute2='a']
    [shortcode atrribute1=true attribute2=true]This is first content[/shortcode]
    [shortcode atrribute1=false]This is second content[/shortcode]
[/parrent_shortcode]

成为:

代码语言:javascript
复制
Array(
    [name] => 'parrent_shortcode'
    [atts] => Array(
        [attribute] => '1'
        [attribute2] => 'a'
    )
    [content] => Array(
        [child1] => Array(
            [name] => 'shortcode'
            [atts] => Array(
                [atrribute1] => true
                [attribute2] => true
            )
            [content] => 'This is first content'
        )
        [child2] => Array(
            [name] => 'shortcode'
            [atts] => Array(
                [atrribute1] => false
            )
            [content] => 'This is second content'
        )
    )
)

另外,短代码可以没有parrent包装器,也可以是单一的(自关闭的)没有内容的。属性中也可以有空格。

我试着用爆炸来完成它,但是有这么多的组合。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-29 23:53:12

代码语言:javascript
复制
function get_pattern( $text ) {
    $pattern = get_shortcode_regex();
    preg_match_all( "/$pattern/s", $text, $c );
    return $c;
}

function parse_atts( $content ) {
    $content = preg_match_all( '/([^ ]*)=(\'([^\']*)\'|\"([^\"]*)\"|([^ ]*))/', trim( $content ), $c );
    list( $dummy, $keys, $values ) = array_values( $c );
    $c = array();
    foreach ( $keys as $key => $value ) {
        $value = trim( $values[ $key ], "\"'" );
        $type = is_numeric( $value ) ? 'int' : 'string';
        $type = in_array( strtolower( $value ), array( 'true', 'false' ) ) ? 'bool' : $type;
        switch ( $type ) {
            case 'int': $value = (int) $value; break;
            case 'bool': $value = strtolower( $value ) == 'true'; break;
        }
        $c[ $keys[ $key ] ] = $value;
    }
    return $c;
}

function the_shortcodes( &$output, $text, $child = false ) {

    $patts = get_pattern( $text );
    $t = array_filter( get_pattern( $text ) );
    if ( ! empty( $t ) ) {
        list( $d, $d, $parents, $atts, $d, $contents ) = $patts;
        $out2 = array();
        $n = 0;
        foreach( $parents as $k=>$parent ) {
            ++$n;
            $name = $child ? 'child' . $n : $n;
            $t = array_filter( get_pattern( $contents[ $k ] ) );
            $t_s = the_shortcodes( $out2, $contents[ $k ], true );
            $output[ $name ] = array( 'name' => $parents[ $k ] );
            $output[ $name ]['atts'] = parse_atts( $atts[ $k ] );
            $output[ $name ]['original_content'] = $contents[ $k ];
            $output[ $name ]['content'] = ! empty( $t ) && ! empty( $t_s ) ? $t_s : $contents[ $k ];
        }
    }
    return array_values( $output );
}

用法:

代码语言:javascript
复制
$text = "[parrent_shortcode attribute='1' attribute2='a b c']
    [shortcode atrribute1=true attribute2=\"j'aime\"]This is first content[/shortcode]
    [shortcode atrribute1=false]This is [shortcode/] content[/shortcode]
[/parrent_shortcode]
";
$output = array();
$output = the_shortcodes( $output, $text );
var_dump( array_values( $output ) );

这与此相呼应:

代码语言:javascript
复制
array (
  0 => 
  array (
    'name' => 'parrent_shortcode',
    'atts' => 
    array (
      'attribute' => 1,
      'attribute2' => 'a b c',
    ),
    'original_content' => '
    [shortcode atrribute1=true attribute2="j\'aime"]This is first content[/shortcode]
    [shortcode atrribute1=false]This is [shortcode/] content[/shortcode]
',
    'content' => 
    array (
      'child1' => 
      array (
        'name' => 'shortcode',
        'atts' => 
        array (
          'atrribute1' => true,
          'attribute2' => 'j\'aime',
        ),
        'original_content' => 'This is first content',
        'content' => 'This is first content',
      ),
      'child2' => 
      array (
        'name' => 'shortcode',
        'atts' => 
        array (
          'atrribute1' => false,
        ),
        'original_content' => 'This is [shortcode/] content',
        'content' => 
        array (
          0 => 
          array (
            'name' => 'shortcode',
            'atts' => 
            array (
            ),
            'original_content' => '',
            'content' => '',
          ),
        ),
      ),
    ),
  ),
)
票数 7
EN

Stack Overflow用户

发布于 2013-09-29 09:52:54

这个短代码API提供了一个名为do_shortcode()的函数--它能满足您的需要吗?函数在这里定义:http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/shortcodes.php#L181

另外,请看一下这里定义的get_shortcode_regex():http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/shortcodes.php#L211,您应该能够以某种方式挖掘解析这些短代码的正则表达式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19076383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档