Php 字符串到多级列表 HTML

Php string to multilevel lists HTML

我有文件

animals.txt

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>

我写了简单的代码,但是我不知道下一步该怎么办,有人能帮我解决吗?

index.php

$file = fopen('animals.txt', 'r');
while (!feof($file)) {
    $line = fgets($file);
    $a = explode(" ", $line);
    $b = str_replace(",", " ", $a);
    $c = explode(" ", $b);
    print_r($b);


}
fclose($file);
?>

如果你只是想读取那个文件而不是写入它,也许使用 file 会比 fopen 更简单,因为它创建了一个行数组。

你需要在每行双重爆炸,一次在列表中分开家庭动物 并第二次将 动物 分开。

要创建结构良好的家庭和动物数组,请尝试以下操作并在不同步骤添加一些 var_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>
function xxAxx(){

    $output='';

    $file = fopen('animals.txt', 'r');
    while (!feof($file))
    {
        $line = fgets($file);
        //replace white spaces with a different delimiter
        $line = preg_replace('/\s+/', '-', $line);
        $line = trim($line);
        // explode with your delemeter
        $a = explode("-", $line);
        $category = $a[0];
        $categoryItems = explode(",", $a[1]);

        $output .= "<li>".$category ."<br>";
        $output .= "<ul type='disc'>";
        foreach ($categoryItems as $item) {
            $output .= '<li>'.$item.'</li>';
        }
        $output .= "</ul>";
        $output .= "</li>";

    }
    fclose($file);

    return $output;
}

?>

<ul type="disc">
    <?php echo xxAxx(); ?>
</ul>