【发布时间】:2016-06-15 08:48:39
【问题描述】:
大家早上好, 我正在尝试重新创建一个从数组中提取数据的 html 页面。 这是数组
$MiniSiteStructureArray = array(
'section_1' => array(
'class' => 'main full',
'contents' => array(
'img' => array(
'class' => 'logo',
'src' => 'assets/images/logo-480x176.png'
),
'div' => array(
'class' => 'cover sfondo',
'img' => array(
'class' => 'cover-image',
'src' => 'assets/images/cover-img-header.png'
)
),
'div_1' => array(
'class' => 'cover-mob sfondo-mob',
'img' => array(
'class' => 'cover-image-mob',
'src' => 'assets/images/cover-img-mobile.jpg'
)
),
'a' => array(
'class' => 'button order light',
'div' => array(
'class' => 'data-min',
'div' => array(
'class' => 'label-holder',
'img' => array(
'src' => 'assets/images/sfondo-cta.jpg'
),
'p' => array(
'class' => 'label',
'strong' => 'text'
)
),
'div_1' => array(
'class' => 'price-holder'
)
)
),
'div_2' => array(
'class' => 'cta',
'p' => array(
'class' => 'main-title',
'a' => array(
'strong' => 'text'
)
)
),
'img_1' => array(
'class' => 'main-title',
'src' => 'assets/images/extra_1.png'
)
)
),
'section_2' => array(
'class' => 'gallery',
'id' => 'video',
'contents' => array(
'p' => array(
'class' => 'title video-title',
'span' => array(
'strong' => 'text'
)
),
'div' => array(
'class' => 'container',
'div' => array(
'class' => 'arrow-container prev',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-sx_1.png'
)
),
'div_1' => array(
'class' => 'arrow-container next',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-dx_1.png'
)
),
'div_2' => array(
'class' => 'gallery-holder',
'div' => array(
'class' => 'internal single',
'div' => array(
'class' => 'gallery-item video',
'div' => array(
'class' => 'wrapper video-wrapper',
'img' => array(
'class' => 'play-vid pointer thumb',
'src' => 'assets/images/img-spot-tv.png'
),
'img_1' => array(
'class' => 'play-icon hover',
'src' => 'assets/images/play.png'
)
)
)
)
)
)
)
),
'section_3' => array(
'class' => 'gallery',
'id' => 'image',
'contents' => array(
'p' => array(
'class' => 'title image-title',
'span' => array(
'strong' => array(
'strong' => 'text'
)
)
),
'div' => array(
'class' => 'container',
'div' => array(
'class' => 'arrow-container prev',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-sx_1.png'
)
),
'div_1' => array(
'class' => 'arrow-container next',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-dx_1.png'
)
),
'div_2' => array(
'class' => 'gallery-holder',
'div' => array(
'class' => 'internal single',
'div' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper image-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/img-1.png'
),
'p' => array(
'class' => 'caption image-caption',
'span' => 'text'
)
)
)
),
'div_1' => array(
'class' => 'internal single',
'div' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper image-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/img-2.png'
),
'p' => array(
'class' => 'caption image-caption',
'span' => 'text'
)
)
)
),
'div_2' => array(
'class' => 'internal single',
'div' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper image-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/img-3.png'
),
'p' => array(
'class' => 'caption image-caption',
'span' => 'text'
)
)
)
),
'div_i' => array(
'class' => 'internal single',
'div' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper image-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/img-i.png'
),
'p' => array(
'class' => 'caption image-caption',
'span' => 'text'
)
)
)
),
)
),
'div_1' => array(
'class' => 'counter'
)
)
),
'section_4' => array(
'class' => 'main',
'id' => 'info',
'contents' => array(
'div' => array(
'class' => 'cover sfondo',
'img' => array(
'class' => 'cover-image',
'src' => 'assets/images/collection-img.jpg'
)
),
'div' => array(
'class' => 'cover-mob sfondo-mob',
'img' => array(
'class' => 'cover-image-mob',
'src' => 'assets/images/collection-img-mobile.jpg'
)
),
'a' => array(
'class' => 'button order light',
'div' => array(
'class' => 'data-min',
'div' => array(
'class' => 'label-holder',
'img' => array(
'src' => 'assets/images/sfondo-cta.jpg'
),
'p' => array(
'class' => 'label',
'strong' => 'text'
)
),
'div_1' => array(
'class' => 'price-holder',
)
)
),
'p' => array(
'class' => 'text-numero-1',
'span' => array(
'strong' => '',
'strong_1' => 'text',
'strong_2' => 'text',
'strong_3' => array(
'class' => 'text-anziche',
'strike' => 'text'
)
)
)
)
),
'section_5' => array(
'class' => 'banner resize',
'id' => 'num1',
'contents' => array(
'div' => array(
'class' => 'cover'
),
'div_1' => array(
'class' => 'container',
'img' => array(
'class' => 'banner-image',
'src' => 'assets/images/number-1.png'
),
'p' => array(
'class' => 'cta num1-cta',
'span' => array(
'a' => 'text'
)
)
)
)
),
'section_6' => array(
'class' => 'gallery',
'id' => 'gifts',
'contents' => array(
'p' => array(
'class' => 'title gifts-title',
'span' => array(
'strong' => array(
'strong' => 'text'
)
)
),
'div' => array(
'class' => 'container',
'div' => array(
'class' => 'arrow-container prev',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-sx_1.png'
)
),
'div_1' => array(
'class' => 'arrow-container next',
'img' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-dx_1.png'
)
),
'div_2' => array(
'class' => 'gallery-holder',
'div' => array(
'class' => 'internal double',
'div' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper gifts-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/gift-1.jpg'
),
'p' => array(
'class' => 'caption gifts-caption',
'span' => 'text'
)
)
),
'div_1' => array(
'class' => 'gallery-item',
'div' => array(
'class' => 'wrapper gifts-wrapper',
'img' => array(
'class' => 'thumb',
'src' => 'assets/images/gift-2.jpg'
),
'p' => array(
'class' => 'caption gifts-caption',
'span' => 'text'
)
)
)
)
)
),
'div_1' => array(
'class' => 'counter'
),
'p' => array(
'class' => 'disclaimer gifts-disclaimer',
'span' => array(
'span' => 'text'
)
)
)
),
'section_7' => array(
'class' => 'footer',
'contents' => array(
'div' => array(
'class' => 'container',
'div' => array(
'class' => 'box',
'div' => array(
'class' => 'wrapper',
'p' => array(
'class' => 'icon pointer',
'img' => array(
'src' => 'assets/images/icn-piano-opera.png'
)
),
'a' => array(
'class' => 'link',
'p' => array(
'class' => 'title',
'span' => 'text'
)
)
)
),
'div_1' => array(
'class' => 'box',
'div' => array(
'class' => 'wrapper',
'p' => array(
'class' => 'icon pointer',
'img' => array(
'src' => 'assets/images/icn-piano-opera.png'
)
),
'a' => array(
'class' => 'link gtm-externallinks',
'p' => array(
'class' => 'title',
'span' => 'text'
)
)
)
)
)
)
),
'section_8' => array(
'class' => 'partners resize',
'contents' => array(
'div' => array(
'class' => 'container',
'img' => array(
'class' => 'partner',
'src' => 'assets/images/logo-dinsey-lucasfilm_2_1.png'
),
'img_1' => array(
'class' => 'partner',
'src' => 'assets/images/logo-mondadori_1_1.png'
)
)
)
),
'section9' => array(
'class' => 'partners-light',
'contents' => array(
'div' => array(
'class' => 'container',
'img' => array(
'class' => 'partner',
'src' => 'assets/images/copyrightLucasLight.png'
)
)
)
));
foreach ($MiniSiteStructureArray as $section => $sectionStructure) {
foreach ($sectionStructure['contents'] as $tag => $contents) {
$tags[] = $tag;
}
$a = formatTag($tags);
$sections[] = $section;
}
$b = formatTag($sections);
如您所见,html 标签的格式不正确(例如 div 和 div_1),所以我创建了这个函数
function formatTag($tags) {
$correctTags = array();
foreach ($tags as $tag) {
switch ($tag) {
case strpos($tag, "img"):
$tag = "img";
break;
case strpos($tag, "div"):
$tag = "div";
break;
case strpos($tag, "a"):
$tag = "a";
break;
case strpos($tag, "p"):
$tag = "p";
break;
case strpos($tag, "section"):
$tag = "section";
break;
}
array_push($correctTags, $tag);
}
return $correctTags;
}
所以我的想法是使用新键(格式良好的标签)重新创建数组。
但现在我被困住了。
假设newKeysArray 完成并假设创建section_1。
我能怎么做?
谢谢
编辑:新数组
$structureArray = array(
'tag' => 'section_8',
'class' => 'partners resize',
'contents' => array(
'tag' => 'div',
'class' => 'container',
'contents' => array(
'tag' => 'img',
'class' => 'partner',
'src' => 'assets/images/logo-dinsey-lucasfilm_2_1.png'
)
)
);
更好?
Edit_2 : NewNewArray
$structureArray = array(
'tag' => array(
'tagName' => 'section_1',
'class' => 'main full',
'contents' => array(
'tag' => array(
'tagName' => 'img',
'class' => 'logo',
'src' => 'assets/images/logo-480x176.png'
),
),
),
'tag_1' => array(
'tagName' => 'div',
'class' => 'cover sfondo',
'contents' => array(
'tag' => array(
'tagName' => 'img',
'class' => 'cover-image',
'src' => 'assets/images/cover-img-header.png'
)
),
),
);
这应该更好,你不觉得吗? 在这种情况下,我手动增加了关键标签,因为在 html 页面中它没有出现
Edit_3_I_hope:NewNewNewArray
$structureArray = array(
'tag' => array(
'tagName' => 'section_1',
'tagAttributes' => array(
'class' => 'main full',
),
'contents' => 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',
),
'contents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'cover-image',
'src' => 'assets/images/cover-img-header.png'
)
)
),
),
);
现在看来很不错
【问题讨论】:
-
你是怎么得到这个数组的?这显然是有缺陷的。如果有迹象表明某物是标签还是属性,那就太好了。您无法控制生成数组的代码?
-
我已经创建了基于 html 页面的数组。如何提高阵列的质量?例如'标签' => 'img'?这样key标签可以帮我找回数据吗?
-
嗯,不错,是的,改进数组生成器要好得多,这样转回html更容易。是的,您的新数组略有改进,但现在您将遇到大量存在的属性的问题:“类”当然不是唯一的。但是为什么要把 html 变成一个数组,然后再变成 html 呢?你知道已经有 php 的 html 解析器了吗?
-
是的,我知道。显然数组键必须不同,我正在研究它。 “但是为什么要把 html 变成一个数组,然后再变成 html 呢?” 1)我正在尝试操作多维数组,练习。 2) 是的,这是最终解析器的开始。你认为这是个坏主意吗?
-
不,不是个坏主意。尤其是如果它是一个练习。尝试更系统一点: 1. HTML 存在于标签之外。 2. 标签有名称、属性和内容。 3. 属性有名称和值。我将在答案中显示另一个数组......让我写它。
标签: php html arrays foreach tags