0
Hey there, guys. I’m trying to get PHP information from an HTML table on a website, but I’m not able to separate the extracted data. My intention is to generate a Json file with this information separated according to each row and HTML tag of the table.
Follow the table and the code I made using explode
.
<ul class="milestones">
<li>
<img src="https://imagem.png">
<span class="out">04/08/2020 10:09</span>
<strong>Entrega</strong>
<br>
IPATINGA/MG
<br>
<small>3 semanas</small>
</li>
<li>
<img src="https://imagem.png">
<span class="out">04/08/2020 10:09</span>
<strong>Entrega</strong>
<br>
SÃO PAULO/SP
<br>
<small>3 semanas</small>
</li>
<li>
<img src="https://imagem.png">
<span class="out">04/08/2020 10:09</span>
<strong>Entrega</strong>
<br>
GOIANIA/GO
<br>
<small>3 semanas</small>
</li>
</ul>
<?php
$url = 'https://meusite.com.br/tabela';
$dadosSite = file_get_contents($url);
$var1 = explode('<ul class="milestones">',$dadosSite);
$var2 = explode('</ul>',$var1[1]);
$var3 = explode('<li>',$var2[0]);
$var4 = explode('</li>',$var3[1]);
$dados_json = json_encode($var3[1]);
$fp = fopen("dados.json", "a");
$escreve = fwrite($fp, $dados_json);
fclose($fp);
?>
The idea of the Json file is to look like this:
[
{"imagem":"https://imagem.png","data_hora":"04/08/2020 10:09","titulo":"Entrega", "sub_titulo":"IPATINGA/MG", "semanas":"3 semanas"},
{"imagem":"https://imagem.png","data_hora":"04/08/2020 10:09","titulo":"Entrega", "sub_titulo":"SÃO PAULO/SP", "semanas":"3 semanas"},
{"imagem":"https://imagem.png","data_hora":"04/08/2020 10:09","titulo":"Entrega", "sub_titulo":"GOIANIA/GO", "semanas":"3 semanas"},
]
Perfect! This is exactly how I intended, but the HTML table is on a site outside the server, how would you look to capture using the site URL? Thank you for your resolution!
– Sulivan Santos
One way would be to take the content with file_get_contents(url) .... I thank you to mark the answer as chosen, this is the community’s way to identify the solutions found to the problems raised
– Ademir Mazer Jr - Nuno