2
I would like to take the content of the site, remove only the text and insert my tags, but in this code I did, when he finds the text "Art" he does not leave the if, and then, only the first ones get the tag li, the rest all get the tag ul.
Could someone help me
# Use the Curl extension to query Google and get back a page of results $url = "www.planalto.gov.br/ccivil_03/constituicao/constituicaocompilado.htm"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $html = curl_exec($ch); curl_close($ch); # Create a DOM parser object $dom = new DOMDocument(); # Parse the HTML from Google. # The @ before the method call suppresses any warnings that # loadHTML might throw because of invalid HTML in the page. @$dom->loadHTML($html); # Iterate over all the tags foreach($dom->getElementsByTagName('font') as $link) { $mystring = $link->nodeValue; $findme = 'Art'; $pos = strpos($mystring, $findme); if ($pos === false) { echo "li"; echo $link->nodeValue; echo "/li"; } else { echo "/ul"; echo "ul id='' class='artigo'"; echo "li"; echo $link->nodeValue; echo "/li"; } }
for the end result to be so
_ul id="titulo1" class="titulo"> _h3>TÍTULO I_/h3> _p>Dos Princípios Fundamentais_/p> _/ul> _ul id="titulo1_artigo1" class="artigo"> _li> _ul class="caput"> _li> Art. 1º A República ... tem como fundamentos: _/li> _/ul> _/li> _li> _ul class="incisos"> _li> I - a soberania;_/li> _li> II - a cidadania_/li> _li> III - o pluralismo político._/li> _/ul> _/li> _li> _ul class="paragrafos"> _li>Parágrafo único. Todo o ... desta Constituição. _/li> _/ul> _/li> _/ul> _ul id="titulo1_artigo2" class="artigo"> _li> _ul class="caput"> _li> Art. 2º São Poderes da União, independentes e harmônicos entre si, o Legislativo, o Executivo e o Judiciário. _/li> _/ul> _/li> _/ul>
For me all worked normally, until the end appears
li.../li
, as it appearsul id='' class='artigo'
– Guilherme Nascimento
For each line, he should see if he has the word article and apply ul only to articles, but he applies ul to lines that do not have the string "Art"
– Alê Moraes
But this is because the
ul
is in theelse
, I think you better specify how you want to display the data, for example you want to display the title and then the content of the article? That’s it?– Guilherme Nascimento
But it would not be this correct uro, it checks if it has the word I look for in each $link, if the search is false, it will insert in the $link, only the tag li, but if it finds the word it excesses the part of Else and assigns only to the $link that ul?
– Alê Moraes
Not the part of
else
is an exception, as there are several tags<font>
the loop can present anything after if, tell me exactly what you want to bring, because I believe you can better use the APIDOMDocument
to facilitate the work, just need to understand what REALLY you want to display and order. Edit the question to add the data.– Guilherme Nascimento
The Constitution has, chapters, which has sections, which has articles, which has incisions etc., so I thought I’d wrap it in a nested ul (I don’t know the name in English), so in the programming check it is the title 4 and the title id 4, if it is article 5, in a ul of id Artigo5.
– Alê Moraes
for example put a template of what I imagine in the original question
– Alê Moraes
I think it gave to have a notion for his example, I believe that of to do everything using only
DOMDocument
, needlesslystrpos
, as it is somewhat time-consuming, tomorrow afternoon I try to post a reply friend, if no one has replied.– Guilherme Nascimento
Got it, William. Vlw man
– Alê Moraes
Good morning Ale, as you can see provided an answer and example, if you have not clear you can ask, to ask questions.
– Guilherme Nascimento