0

I have file

animals.txt

fishes   shark,tuna,salmon
birds    parrot,pigeon
mammals  cow,dog,cat

In every line, for example between fishes and shark is tabulation.

I wanna get this output:

<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>

I wrote simple code, but I don't know what can I do next, can someone help me solve it?

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);
?>
2
  • 2
    What have you tried? It looks like you made a start and then just.... stopped. My only point at this stage is that you should be exploding on comma in your second explode, not replacing commas with spaces and splitting on spaces. Commented Jan 14, 2016 at 17:29
  • I know, but im looking for clue or something Commented Jan 14, 2016 at 17:38

2 Answers 2

1

If you're simply going to read that file and not write in it, maybe using file will be simpler than fopen as it creates an array of lines.

You'll want to double explode every line, once on the tabulation to separate family and animals and a second time to split animals individually.

To create a nicely structure array of families and animals, try the following and add some var_dump at different steps to see the logic:

$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;
    }
}

The groupedAnimals array should be populated like that:

Array
(
    [fishes] => Array
        (
            [0] => shark
            [1] => tuna
            [2] => salmon

        )

    [birds] => Array
        (
            [0] => parrot
            [1] => pigeon

        )

    [mammals] => Array
        (
            [0] => cow
            [1] => dog
            [2] => cat
        )

)

Once the groupedAnimals array is populated, you can simply print them the way you want, following your template:

<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>
Sign up to request clarification or add additional context in comments.

Comments

1
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.