Parse Wordpress like Shortcode

前端 未结 6 1775
梦如初夏
梦如初夏 2020-12-15 10:43

I want to parse shortcode like Wordpress with attributes:

Input:

[include file=\"header.html\"]

I need output as array, function na

6条回答
  •  忘掉有多难
    2020-12-15 11:25

    I have modified above function with wordpress function

    function extractThis($short_code_string) {
        $shortocode_regexp = "/(?P(?:(?:\\s?\\[))(?P[\\w\\-]{3,})(?:\\s(?P[\\w\\d,\\s=\\\"\\'\\-\\+\\#\\%\\!\\~\\`\\&\\.\\s\\:\\/\\?\\|]+))?(?:\\])(?:(?P[\\w\\d\\,\\!\\@\\#\\$\\%\\^\\&\\*\\(\\\\)\\s\\=\\\"\\'\\-\\+\\&\\.\\s\\:\\/\\?\\|\\<\\>]+)(?:\\[\\/[\\w\\-\\_]+\\]))?)/u";
        preg_match_all($shortocode_regexp, $short_code_string, $matches, PREG_SET_ORDER);
        $shortcodes = array();
        foreach ($matches as $i => $value) {
           $shortcodes[$i]['shortcode'] = $value['shortcode'];
           $shortcodes[$i]['name'] = $value['name'];
           if (isset($value['attrs'])) {
            $attrs = shortcode_parse_atts($value['attrs']);
            $shortcodes[$i]['attrs'] = $attrs;
           }
           if (isset($value['content'])) {
            $shortcodes[$i]['content'] = $value['content'];
           }
        }
        return $shortcodes;
      }
    

    I think this one help for all :)

提交回复
热议问题