Transform into ul

Asked

Viewed 93 times

2

Hello, all right?

PHP can take tabs-formatted text and turn it into a ul?

José
    Dados
        Idade: 30
        Estado Civil: Solteiro
    Hobbies
        Tocar violão
            Rock
            Blues
        Ler
        Viajem
            Praia

And I’d like to turn to look like this:

    <ul>
        <li>José
            <ul>
                <li>Dados
                    <ul>
                        <li>Idade: 30</li>
                        <li>Estado Civil: Solteiro</li>
                    </ul>               
                </li>
                <li>Hobbies
                    <ul>
                        <li>Tocar violão
                            <ul>
                                <li>Rock</li>
                                <li>Blues</li>
                            </ul>                      
                        </li>
                        <li>Ler</li>
                        <li>Viajem
                            <ul>
                                <li>Praia</li>
                                <li>Montanha</li>
                            </ul>                      
                        </li>                      
                    </ul>               
                </li>
            </ul>       
        </li>
    </ul>
  • Where does this text come from?

  • From a client, now I don’t know how he does these texts, there is some way to search for tabs, for example, a preg_replace that analyzes if he has a tab, he does a routine, two tabs does another and so on

  • Take a look at my answer. I was able to identify the tab.

  • I tended to use REGEX but could not. Maybe @dvd can.

  • 2

    @Andreicoelho It seems to me that your answer killed the question.

1 answer

4


There are several ways to do this. I created this example in a simpler way, but I could have created a recursive function, for example. I did more for the challenge. =)

This is the file text.txt that will be dealt with file_get_contents("texto.txt")

José
    Dados
        Idade: 30
        Estado Civil: Solteiro
    Hobbies
        Tocar violão
            Rock
            Blues
        Ler
        Viajem
            Praia

However, I did a test using string, and it reads normally, like this:

$string = "José
    Dados
        Idade: 30
        Estado Civil: Solteiro
    Hobbies
        Tocar violão
            Rock
            Blues
        Ler
        Viajem
            Praia";

No more... follow the code commented:

$string =  file_get_contents("texto.txt"); // pega o conteudo
$string = explode("\n", $string); // separa as linhas
$array = array();

// cria o array com os níveis com contagem da tabulação
foreach($string as $linha){
    $num_spaces = substr_count($linha, " ");
    $tabs_count = floor($num_spaces / 4); // quantidade de tabulação
    $array[] = array( $tabs_count => $linha );
}

// mostra o menu
echo "<ul>";
for($x = 0; $x < count($array); $x++){
    $menu = $array[$x];
    foreach($menu as $key => $val){
        $chave = $key;
        $valor = $val;
    }
    if(isset($array[$x + 1])){
        $next = $array[$x + 1];
        foreach($next as $key => $val){
        $nextChave = $key;
        }
        if ($nextChave > $chave){
            echo "<li>".trim($valor);
            echo "<ul>";
        } else {
            if($nextChave == $chave){
                echo "<li>".trim($valor)."</li>";
            } else {
                echo "<li>".trim($valor)."</li></ul>";
            }
        }
    } else {
        echo "<li>".trim($valor)."</li>";
    }
}
echo "</ul>";
  • 1

    Thanks Andrei, helped a lot man!!!

  • @Call here if you need anything! Phone!

  • 1

    Mt good! Congratulations really!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.