2
I have the following code:
<?php
function obterID($string) {
$res = preg_match('~id="([\w]+)"~i', $string, $IDs);
if ($res){
return $IDs[1];
} else {
return "";
}
}
// Lê conteúdo do txt
$documento = '<ul id="titulo1" class="titulo">
<li>
<p>TÍTULO I</p>
</li>
</ul>
<ul id="artigo1" class="artigo">
<li>
<p>Art. 1º...</p>
</li>
</ul>
<ul id="artigo2" class="artigo">
<li>
<p>Art. 2º ....</p>
</li>
</ul>
<ul id="artigo3" class="artigo">
<li>
<p>Art. 3º ....:</p>
</li>
</ul>
<ul id="artigo4" class="artigo">
<li>
<p>Art. 4º ....:</p>
</li>
</ul>
<ul id="titulo2" class="titulo">
<li>
<p>TÍTULO II</p>
</li>
</ul>
<ul id="capitulo1" class="capitulo">
<li>
<p>CAPÍTULO I</p>
</li>
</ul>
<ul id="artigo5" class="artigo">
<li>
<p>Art. 5º ....:</p>
</li>
</ul>
<ul id="capitulo2" class="capitulo">
<li>
<p>CAPÍTULO II</p>
</li>
</ul>
<ul id="artigo6" class="artigo">
<li>
<p>Art. 6º ...</p>
</li>
</ul>
<ul id="artigo7" class="artigo">
<li>
<p>Art. 7º ...</p>
</li>
</ul>
<ul id="artigo8" class="artigo">
<li>
<p>Art. 8º....:</p>
</li>
</ul>
<ul id="artigo9" class="artigo">
<li>
<p>Art. 9º É as.</p>
</li>
</ul>
<ul id="artigo10" class="artigo">
<li>
<p>Art. 10. ....</p>
</li>
</ul>
<ul id="artigo11" class="artigo">
<li>
<p>Art. 11. ....</p>
</li>
</ul>
<ul id="capitulo3" class="capitulo">
<li>
<p>CAPÍTULO III</p>
</li>
</ul>
<ul id="artigo12" class="artigo">
<li>
<p>Art. 12. São brasileiros:</p>
</li>
</ul>';
$linhas = explode("<ul", $documento);
$novalinhas = "";
$concatenaTitulo = "";
foreach ($linhas as $novalinha){
$novoTituloSlug = ObterID($novalinha);
if (empty($novoTituloSlug))
continue;
$concatenaTitulo .= $novoTituloSlug;
}
echo $concatenaTitulo;
?>
DEMO
I would like to concatenate by hierarchy, as in a breadcrumb (but I don’t know how to make a breadcrumb).
This is the result I would like in each foreach
:
titulo1
titulo1artigo1
titulo1artigo2
titulo1artigo3
titulo2
titulo2capitulo1
titulo2capitulo1artigo5
titulo2capitulo2
titulo2capitulo7artigo6
But it only concatenates, without substituting:
titulo1
titulo1artigo1
titulo1artigo1artigo2
titulo1artigo1artigo2artigo3
And it goes all the way to:
titulo1artigo1artigo2artigo3artigo4titulo2capitulo1artigo5capitulo2artigo6artigo7artigo8artigo9artigo10artigo11capitulo3artigo12
But so, he did not concaten, since way
titulo1
titulo1artigo1
titulo1artigo2
titulo2
titulo2capitulo1
titulo2capitulo1artigo5
– Alê Moraes
I believe so, because they are uls with lis inside him
– Alê Moraes
Yes, because, I see that the laws do not pass ul with ul within it. So I would like to structure them like this, you have another view on the structure?
– Alê Moraes
It’s expensive, then look at the official page of the constitution, got to my head, kkkk. Because the structure varies a lot, and I want to do with the other laws that also vary a lot. kkkk, but if you can, then look at the constitution and tell me, what is that formatting, I am not a beast, but it seems to me very silly, and if you have any suggestions to hierarchize correctly, I thank you, kkk
– Alê Moraes
@Alêmoraes Bah, But the hierarchy of the example is correct? The page you say is that one? I can’t figure out how to create this hierarchy that you want. =/
– stderr
The hierarchy is this same, for example, the title 1, only has 4 articles, has nothing else, already the title 2 has 5 chapter and within the chapters has some articles, another example, in title 3, chapter 5, there are two sections with some articles inside
– Alê Moraes
@Alêmoraes Veja that image, based on the HTML posted in question this hierarchy is correct?
– stderr
From what I’ve seen of your image, I don’t think you’d have the first two lines, because from what I understand, it looks like title 1 and title 2 are within the title.
– Alê Moraes
@Alêmoraes I edited the answer.
– stderr
Our man, it worked, wow wow. I will study your code to understand your reasoning, deserves all votes!!!
– Alê Moraes