PHP如何关闭这个数组中的html标签? [英] PHP How can close the html tags from this array?
问题描述
我有那个带有html标签的数组,我需要用PHP创建一个带有某个函数的输出,但是我不能使它正确,我需要一些帮助来使这个数组关闭标签正确。
这个数组已经逐行建立,但是我不能让父母成为父母。
输出需要:
< form class =form-horizontal>
< div class =firstclass> FIRSTCLASS< / div>
< div class =form-group>
< label class =col-sm-3 control-label> TEXT
< div class =col-sm-9>
< span class =myspan> MYSPAN
< div class =myclass> MYCLASS
< / div>
< / span>
< / div>
< / label>
< / div>
< div class =firstclass> FIRSTCLASS< / div>
< div class =form-group>
< label class =col-sm-3 control-label> TEXT
< div class =col-sm-9>
< span class =myspan> MYSPAN
< div class =myclass> MYCLASS
< / div>
< / span>
< / div>
< / label>
< / div>
< div class =firstclass> FIRSTCLASS< / div>
< / form>
PHP数组已经建成:
$ data = [
[
'tag'=> 'form',
'options'=> 'class =form-horizontal',
'text'=>假,
'等级'=> 1,
'parents'=>真,
],
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
'level'=> 2,
'parents'=>假,
],
[
'tag'=> 'div',
'options'=> 'class =form-group',
'text'=> 'FORMGROUP',
'level'=> 2,
'parents'=>真,
],
[
'tag'=> 'label',
'options'=> 'class =col-sm-3 control-label',
'text'=> 'TEXT',
'level'=> 3,
'parents'=>真,
],
[
'tag'=> 'div',
'options'=> 'class =col-sm-9',
'text'=>假,
'等级'=> 4,
'parents'=>真
],
[
'tag'=> 'span',
'options'=> 'class =myspan',
'text'=> 'MYSPAN',
'level'=> 5,
'parents'=>真
],
[
'tag'=> 'div',
'options'=> 'class =myclass',
'text'=> 'MYCLASS',
'level'=> 6,
'parents'=>假
],
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
'level'=> 2,
'parents'=>假
],
[
'tag'=> 'div',
'options'=> 'class =form-group',
'text'=> 'FORMGROUP',
'level'=> 2,
'parents'=>真
],
[
'tag'=> 'label',
'options'=> 'class =col-sm-3 control-label',
'text'=> 'TEXT',
'level'=> 3,
'parents'=>真
],
[
'tag'=> 'div',
'options'=> 'class =col-sm-9',
'text'=>假,
'等级'=> 4,
'parents'=>真
],
[
'tag'=> 'span',
'options'=> 'class =myspan',
'text'=> 'MYSPAN',
'level'=> 5,
'parents'=>真
],
[
'tag'=> 'div',
'options'=> 'class =myclass',
'text'=> 'MYCLASS',
'level'=> 6,
'parents'=>假
],
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
'level'=> 2,
'parents'=>假
]
];
这是带有错误输出的函数:
function get($ data){
$ top ='';
$ botttom ='';
foreach($ data as $ key => $ row){
$ tag = isset($ row ['tag'])? $ row ['tag']:false;
$ text = isset($ row ['text'])? $ row ['text']:false;
$ level = isset($ row ['level'])? $ row ['level']:false;
$ options = isset($ row ['options'])? $ row ['options']:false;
$ parents = isset($ row ['parents'])? $ row ['parents']:false;
$ openTag ='<'。$ tag。$ options。'>'。$ text;
$ closeTag ='< /'.$ tag。'>';
if($ parents){
$ top。= $ openTag;
} else {
$ botttom = $ closeTag。$ botttom;
}
}
$ result = $ top。$ botttom;
返回$ result;
}
echo(get($ data));
嵌套数组和使用递归可能如下所示: / p>
<?php
$ data = [
[
'tag'=> 'form',
'options'=> 'class =form-horizontal',
'text'=> false,
'items'=> [
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
],
[
'tag'=> 'div',
'options'=> 'class =form-group',
'text'=> 'FORMGROUP',
'items'=> [
[
'tag'=> 'label',
'options'=> 'class =col-sm-3 control-label',
'text'=> 'TEXT',
'items'=> [
[
'tag'=> 'div',
'options'=> 'class =col-sm-9',
'items'=> [
[
'tag'=> 'span',
'options'=> 'class =myspan',
'text'=> 'MYSPAN',
'items'=> [
[
'tag'=> 'div',
'options'=> 'class =myclass',
'text'=> 'MYCLASS',
],
],
],
],
],
],
],
],
],
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
],
[
'tag'=> 'div',
'options'=> 'class =form-group',
'text'=> 'FORMGROUP',
'items'=> [
[
'tag'=> 'label',
'options'=> 'class =col-sm-3 control-label',
'text'=> 'TEXT',
'items'=> [
[
'tag'=> 'div',
'options'=> 'class =col-sm-9',
'items'=> [
[
'tag'=> 'span',
'options'=> 'class =myspan',
'text'=> 'MYSPAN',
'items'=> [
[
'tag'=> 'div',
'options'=> 'class =myclass',
'text'=> 'MYCLASS',
],
],
],
],
],
],
],
],
],
[
'tag'=> 'div',
'options'=> 'class =firstclass',
'text'=> 'FIRSTCLASS',
],
],
],
];
生成html的代码如下:
<?php
function get($ data){
$ result ='';
foreach($ data as $ key => $ row){
$ tag = array_key_exists('tag',$ row)? $ row ['tag']:false;
$ text = array_key_exists('text',$ row)? $ row ['text']:'';
$ options = array_key_exists('options',$ row)? $ row ['options']:'';
$ nested = array_key_exists('items',$ row)? get($ row ['items'])):'';
if(false === $ tag){
continue; //可能会抛出异常或错误处理
$ b $ result。=< {$ tag} {$ options}> {$ text} {$ nested}< / {$标签}>中;
}
返回$ result;
}
echo(get($ data));
嵌套原始数据是嵌套函数:
函数nest(array $ data = array()){
$ level = null;
$ last = null;
foreach($ data as $ index => $ item){
$ level = $ level ?? $项目[级别];
if(null!== $ last&& $ item ['level']> $ level){
$ data [$ last] ['items'] [] = $ item;
unset($ data [$ index]);
}
if($ item ['level'] == $ level){
$ last = $ index;
foreach($ data as $ index => $ item){
if(array_key_exists('items',$ item)){
$ data [$ index] ['items'] = nest($ item ['items']);
}
}
返回$ data;
}
print_r(nest($ data)); die();
I have that array with html tags and i need make an output with some function with PHP but i can not make it correct i need some help to make this array closing tags correct.
This array is already builded line by line but i can not make the parents.
The OUTPUT NEEDED:
<form class="form-horizontal">
<div class="firstclass">FIRSTCLASS</div>
<div class="form-group">
<label class="col-sm-3 control-label">TEXT
<div class="col-sm-9">
<span class="myspan">MYSPAN
<div class="myclass">MYCLASS
</div>
</span>
</div>
</label>
</div>
<div class="firstclass">FIRSTCLASS</div>
<div class="form-group">
<label class="col-sm-3 control-label">TEXT
<div class="col-sm-9">
<span class="myspan">MYSPAN
<div class="myclass">MYCLASS
</div>
</span>
</div>
</label>
</div>
<div class="firstclass">FIRSTCLASS</div>
</form>
The PHP array have already builded:
$data = [
[
'tag' => 'form',
'options' => ' class="form-horizontal"',
'text' => false,
'level' => 1,
'parents' => true,
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false,
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'level' => 2,
'parents' => true,
],
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'level' => 3,
'parents' => true,
],
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'text' => false,
'level' => 4,
'parents' => true
],
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'level' => 5,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
'level' => 6,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'level' => 2,
'parents' => true
],
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'level' => 3,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'text' => false,
'level' => 4,
'parents' => true
],
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'level' => 5,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
'level' => 6,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false
]
];
And this is the function with incorrect output:
function get($data) {
$top= '';
$botttom= '';
foreach($data as $key => $row) {
$tag = isset($row['tag']) ? $row['tag'] : false;
$text = isset($row['text']) ? $row['text'] : false;
$level = isset($row['level']) ? $row['level'] : false;
$options = isset($row['options']) ? $row['options'] : false;
$parents = isset($row['parents']) ? $row['parents'] : false;
$openTag = '<'.$tag.$options.'>'.$text;
$closeTag = '</'.$tag.'>';
if($parents) {
$top.= $openTag;
} else {
$botttom = $closeTag.$botttom;
}
}
$result = $top.$botttom;
return $result;
}
echo(get($data));
Nesting arrays and using recursion may look like this:
<?php
$data = [
[
'tag' => 'form',
'options' => ' class="form-horizontal"',
'text' => false,
'items' => [
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'items' => [
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'items' => [
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'items' =>[
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'items' => [
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
],
],
],
],
],
],
],
],
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'items' => [
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'items' => [
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'items' => [
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'items' => [
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
],
],
],
],
],
],
],
],
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
],
],
],
];
And code for generating html like this:
<?php
function get($data) {
$result = '';
foreach ($data as $key => $row) {
$tag = array_key_exists('tag', $row) ? $row['tag'] : false;
$text = array_key_exists('text', $row) ? $row['text'] : '';
$options = array_key_exists('options', $row) ? $row['options'] : '';
$nested = array_key_exists('items', $row) ? get($row['items'])) : '';
if (false === $tag) {
continue; // May throw exception or some error handling
}
$result .= "<{$tag} {$options}>{$text}{$nested}</{$tag}>";
}
return $result;
}
echo(get($data));
To nest your original data here is the nesting function:
function nest(array $data = array()) {
$level = null;
$last = null;
foreach ($data as $index => $item) {
$level = $level ?? $item['level'];
if (null !== $last && $item['level'] > $level) {
$data[$last]['items'][] = $item;
unset($data[$index]);
}
if ($item['level'] == $level) {
$last = $index;
}
}
foreach ($data as $index => $item) {
if (array_key_exists('items', $item)) {
$data[$index]['items'] = nest($item['items']);
}
}
return $data;
}
print_r(nest($data));die();
这篇关于PHP如何关闭这个数组中的html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!