【问题标题】:Recreate html page from array (2.0)从数组重新创建 html 页面 (2.0)
【发布时间】:2016-06-16 10:20:10
【问题描述】:

大家早上好, 昨天我问了一个问题来解决这个问题,但数组有缺陷。 现在数组似乎已修复,但我在创建页面时遇到了麻烦。 所以,这是固定数组

'tag' => array(
    'tagName' => 'section',
    'tagAttributes' => array(
        'class' => 'main full',
    ),
    'tagContents' => array(
        'tag' => array(
            'tagName' => 'img',
            'tagAttributes' => array(
                'class' => 'logo',
                'src' => 'assets/images/logo-480x176.png'
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'cover sfondo'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'img',
                    'tagAttributes' => array(
                        'class' => 'cover-image',
                        'src' => 'assets/images/cover-img-header.png'
                    )
                )
            )
        )
    ),
    'tag_2' => array(
        'tagName' => 'div',
        'tagAttributes' => array(
            'class' => 'cover-mob sfondo-mob'
        ),
        'tagContents' => array(
            'tag' => array(
                'tagName' => 'img',
                'tagAttributes' => array(
                    'class' => 'cover-image-mob',
                    'src' => 'assets/images/cover-img-mobile.jpg'
                )
            )
        )
    ),
    'tag_3' => array(
        'tagName' => 'a',
        'tagAttributes' => array(
            'class' => 'button order light'
        ),
        'tagContents' => array(
            'tag' => array(
                'tagName' => 'div',
                'tagAttributes' => array(
                    'class' => 'data-min',
                ),
                'tagContents' => array(
                    'tag' => array(
                        'tagName' => 'div',
                        'tagAttributes' => array(
                            'class' => 'label-holder',
                        ),
                        'tagContents' => array(
                            'tag' => array(
                                'tagName' => 'img',
                                'tagAttrbiutes' => array(
                                    'src' => 'assets/images/sfondo-cta.jpg'
                                )
                            ),
                            'tag_1' => array(
                                'tagName' => 'p',
                                'tagAttributes' => array(
                                    'class' => 'label',
                                ),
                                'tagContents' => array(
                                    'strong' => 'text'
                                )
                            )
                        )
                    ),
                    'tag_1' => array(
                        'tagName' => 'div',
                        'tagAttributes' => array(
                            'class' => 'price-holder'
                        )
                    )
                )
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'cta'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'p',
                    'tagAttributes' => array(
                        'class' => 'main-title'
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'a',
                            'tagAttributes' => array(
                                'strong' => 'text'
                            )
                        )
                    )
                )
            )
        ),
        'tag_2' => array(
            'tagName' => 'img',
            'tagAttributes' => array(
                'class' => 'main-title',
                'src' => 'assets/images/extra_1.png'
            )
        )
    )
),
'tag_1' => array(
    'tagName' => 'section',
    'tagAttributes' => array(
        'class' => 'gallery',
        'id' => 'video'
    ),
    'tagContents' => array(
        'tag' => array(
            'tagName' => 'p',
            'tagAttributes' => array(
                'class' => 'title video-title',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'span',
                    'tagAttributes' => array(
                        'strong' => 'text'
                    )
                )
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'container',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'div',
                    'tagAttributes' => array(
                        'class' => 'arrow-container prev',
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'img',
                            'tagAttributes' => array(
                                'class' => 'arrow',
                                'src' => 'assets/images/freccia-sx_1.png'
                            )
                        )
                    )
                )
            )
        ),
        'tag_2' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'arrow-container next',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'img',
                    'tagAttributes' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-dx_1.png'
                    )
                )
            )
        ),
        'tag_3' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'gallery-holder'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'div',
                    'tagAttributes' => array(
                        'class' => 'internal single'
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'div',
                            'tagAttributes' => array(
                                'class' => 'gallery-item video',
                            ),
                            'tagContents' => array(
                                'tag' => array(
                                    'tagName' => 'div',
                                    'tagAttributes' => array(
                                        'class' => 'wrapper video-wrapper'
                                    ),
                                    'tagContents' => array(
                                        'tag' => array(
                                            'tagName' => 'img',
                                            'tagAttributes' => array(
                                                'class' => 'play-vid pointer thumb',
                                                'src' => 'assets/images/img-spot-tv.png'
                                            )
                                        ),
                                        'tag_1' => array(
                                            'tagName' => 'img',
                                            'tagAttributes' => array(
                                                'class' => 'play-icon hover',
                                                'src' => 'assets/images/play.png'
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )
),

这就是我尝试创建html页面的方式

foreach ($MiniSiteStructureArray as $section => $sectionStructure) {

    $html = '<' . $sectionStructure['tagName'] . '';
    foreach ($sectionStructure['tagAttributes'] as $name => $value) {
        $html .= " $name='" . $value . "'>";
    }
    foreach ($sectionStructure['tagContents'] as $contents) {
        foreach ($contents['tagAttributes'] as $name => $value) {
            $html .= '<' . $contents['tagName'] . ' ' . $name . '="' . $value . '">';
        }
        if (isset($contents['tagContents'])) {
            $html .= findAllContents($contents['tagContents'], $html);
        }
    }
}

function findAllContents($tagContents, $tmpHtml) {
    foreach ($tagContents as $tag => $tagValue) {
        if (isset($tagValue['tagAttributes'])) {
            foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
                $tmpHtml = '<' . $tagValue['tagName'] . ' ' . $tagKey . '="' . $value . '"></' . $tagValue['tagName'] . '>';
            }
        }
    }
    return $tmpHtml;
}

echo $html;

但是有一些问题。

以循环 'tagAttributes' 为例。

1) 代码找到 'class' => 'logo' 但后来它找到 'src' => 'assets/...' 并覆盖以前的值。

2) 与 $html 相同

编辑_1:

   foreach ($MiniSiteStructureArray as $section => $sectionStructure) {
    $html = '<' . $sectionStructure['tagName'] . '';
    foreach ($sectionStructure['tagAttributes'] as $name => $value) {
        $html .= " $name='" . $value . "'>";
    }
    foreach ($sectionStructure['tagContents'] as $contents) {
        $html .= '<' . $contents['tagName'];
        foreach ($contents['tagAttributes'] as $name => $value) {
            $html.=' '. $name . '="' . $value . '">';
        }
        if (isset($contents['tagContents'])) {
            $html .= findAllContents($contents['tagContents'], $html);
        }
    }
    $html .= '</'.$contents['tagName'].'>';   
}

function findAllContents($tagContents, $tmpHtml) {
    foreach ($tagContents as $tag => $tagValue) {
        $tmpHtml = '<' . $tagValue['tagName'];

        if (isset($tagValue['tagAttributes'])) {
            foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
                $tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
            }
        }

        $tmpHtml .= '></' . $tagValue['tagName'] . '>';
    }
    return $tmpHtml;
}

更好,谢谢。

现在仍然是 $html 问题。 我一直想在代码的开头打开一个像 $html = &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt; 这样的 html 标签,并在最后关闭 html 标签和 body 标签,但我试过了,但没有用。

编辑_2:

好的,我想它已经完成了。我只在开头添加了这个$html = ''

【问题讨论】:

    标签: php html arrays foreach tags


    【解决方案1】:

    你必须在tagAttributes foreach 循环之前打开你的标签并在它之后关闭。如果不给数组添加 tagAttributes 键也会有问题,所以标签本身会被省略。

    findAllContents() 修改foreach 循环的示例:

    foreach ($tagContents as $tag => $tagValue) {
        $tmpHtml .= '<' . $tagValue['tagName']
    
        if (isset($tagValue['tagAttributes'])) {
            foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
                $tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
            }
        }
    
        $tmpHtml .= '></' . $tagValue['tagName'] . '>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 2011-09-29
      • 1970-01-01
      • 2012-10-25
      • 2017-06-24
      • 1970-01-01
      • 2014-07-04
      • 2011-09-30
      相关资源
      最近更新 更多