Php字符串到多层次列表HTML
我有文件 动物.txtPhp字符串到多层次列表HTML,php,string,html-lists,Php,String,Html Lists,我有文件 动物.txt fishes shark,tuna,salmon birds parrot,pigeon mammals cow,dog,cat 例如,在鱼和鲨鱼之间的每一行中都有表格 我想得到这个输出: <ul type="disc"> <li>fishes<br> <ul type="disc"> <li>shark
fishes shark,tuna,salmon
birds parrot,pigeon
mammals cow,dog,cat
例如,在鱼和鲨鱼之间的每一行中都有表格
我想得到这个输出:
<ul type="disc">
<li>fishes<br>
<ul type="disc">
<li>shark</li>
<li>tuna</li>
<li>salmon</li>
</ul>
</li>
<li>birds<br>
<ul type="disc">
<li>parrot</li>
<li>pigeon</li>
</ul>
</li>
<li>mammals<br>
<ul type="disc">
<li>cow</li>
<li>dog</li>
<li>cat</li>
</ul>
</li>
</ul>
如果您只是想读取该文件而不在其中写入,那么使用
file
可能比fopen
更简单,因为它会创建一个行数组
您将需要对每行进行两次分解,一次在表格上分离族和动物,第二次单独拆分动物
要创建一个结构良好的家族和动物数组,请尝试以下操作,并在不同的步骤中添加一些var\u dump
,以查看逻辑:
$file = file('animals.txt');
$groupedAnimals = array();
foreach ($file as $line) {
// First explode on tabulations
$line = explode("\t", $line);
$family = $line[0];
// Then explode on commas
$animals = explode(',', $line[1]);
foreach ($animals as $animal) {
$groupedAnimals[$family][] = $animal;
}
}
groupedAnimals
数组的填充方式如下:
Array
(
[fishes] => Array
(
[0] => shark
[1] => tuna
[2] => salmon
)
[birds] => Array
(
[0] => parrot
[1] => pigeon
)
[mammals] => Array
(
[0] => cow
[1] => dog
[2] => cat
)
)
填充groupedAnimals
数组后,您只需按照您的模板以您想要的方式打印它们:
<ul type="disc">
<?php foreach ($groupedAnimals as $family => $animals): ?>
<li>
<?php echo $family ?>
<ul>
<?php foreach ($animals as $animal): ?>
<li><?php echo $animal ?></li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
-
如果您只是想读取该文件而不在其中写入,那么使用文件
可能比fopen
更简单,因为它会创建一个行数组
您将需要对每行进行两次分解,一次在表格上分离族和动物,第二次单独拆分动物
要创建一个结构良好的家族和动物数组,请尝试以下操作,并在不同的步骤中添加一些var\u dump
,以查看逻辑:
$file = file('animals.txt');
$groupedAnimals = array();
foreach ($file as $line) {
// First explode on tabulations
$line = explode("\t", $line);
$family = $line[0];
// Then explode on commas
$animals = explode(',', $line[1]);
foreach ($animals as $animal) {
$groupedAnimals[$family][] = $animal;
}
}
groupedAnimals
数组的填充方式如下:
Array
(
[fishes] => Array
(
[0] => shark
[1] => tuna
[2] => salmon
)
[birds] => Array
(
[0] => parrot
[1] => pigeon
)
[mammals] => Array
(
[0] => cow
[1] => dog
[2] => cat
)
)
填充groupedAnimals
数组后,您只需按照您的模板以您想要的方式打印它们:
<ul type="disc">
<?php foreach ($groupedAnimals as $family => $animals): ?>
<li>
<?php echo $family ?>
<ul>
<?php foreach ($animals as $animal): ?>
<li><?php echo $animal ?></li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
-
函数xxAxx(){
$output='';
$file=fopen('anists.txt','r');
而(!feof($file))
{
$line=fgets($file);
//用不同的分隔符替换空白
$line=preg_replace('/\s+/','-',$line);
$line=修剪($line);
//用你的仪表爆炸
$a=分解(“-”,$line);
$category=$a[0];
$categoryItems=爆炸(“,”,$a[1]);
$output.=“”“$category。”
”;
$output.=“”;
foreach($类别项作为$项){
$output.='- '.$item.
';
}
$output.=“
”;
$output.=“ ”;
}
fclose($文件);
返回$output;
}
?>
函数xxAxx(){
$output='';
$file=fopen('anists.txt','r');
而(!feof($file))
{
$line=fgets($file);
//用不同的分隔符替换空白
$line=preg_replace('/\s+/','-',$line);
$line=修剪($line);
//用你的仪表爆炸
$a=分解(“-”,$line);
$category=$a[0];
$categoryItems=爆炸(“,”,$a[1]);
$output.=“”“$category。”
”;
$output.=“”;
foreach($类别项作为$项){
$output.='- '.$item.
';
}
$output.=“
”;
$output.=“ ”;
}
fclose($文件);
返回$output;
}
?>
您尝试过什么?看起来你刚开始,然后。。。。停止。我在这一阶段的唯一观点是,你应该在第二次分解中用逗号分解,而不是用空格代替逗号,在空格上拆分。我知道,但我在寻找线索或其他东西。你试过什么?看起来你刚开始,然后。。。。停止。我在这一阶段的唯一观点是,你应该在第二次分解中分解逗号,而不是用空格替换逗号,或者在空格上拆分。我知道,但我在寻找线索或其他东西