PHP: How to use preg_match_all in this excerpt?

Asked

Viewed 151 times

1

Site I want to get the data

I need to use preg_match_all to get what’s inside

<table class="grid-table survey-info" cellspacing="0">

So far I’ve done the following

<?php

$url = 'http://metadados.capes.gov.br/index.php/catalog/100';


$dadosSite = file_get_contents($url);

preg_match_all('/(O que falta)/', $dadosSite, $conteudo);

$echo conteudo[0][0];
?>
  • But get what? only the texts? the HTML inside the table?

  • Only the texts themselves

  • See if it concerns you: http://kithomepage.com/sos/ttt.php

  • Perfect, like you did to look like this?

  • I just got it, but if I can send it like you did so I can take a look... VLW for the help

  • I edited the answer, I had posted the wrong code

Show 1 more comment

1 answer

0

I took advantage of the table tags to give a shape to output, example replaces <td class="label"> for Label : and so on. Only td class="value" are all messed up, have <td class="value"> and <td class="value" > etc.

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<?php
$myLine = "";
if(!($myFile=fopen("http://metadados.capes.gov.br/index.php/catalog/100","r"
)))
{
echo "Arquivo não-acessível";
exit;
}
while(!feof($myFile))
{
$myLine .= fgets($myFile,255);
}
fclose($myFile);

$start = '<table class="grid-table survey-info" cellspacing="0">'; //ponto inicial da procura no código fonte da pg
$end = "</table>"; //ponto final da procura no código fonte da pg
$start_position=strpos($myLine, $start);
$end_position=strpos($myLine, $end)+strlen($end);
$length = $end_position - $start_position;
$myLine = substr($myLine, $start_position, $length);

$myLine = str_replace('<table class="grid-table survey-info" cellspacing="0">',"",$myLine);

$myLine = str_replace('</table>',"",$myLine);

$myLine = str_replace('<tr>',"",$myLine);
$myLine = str_replace('</tr>',"<br>",$myLine);

$myLine = str_replace('<tr valign="top"  >',"",$myLine);

$myLine = str_replace('<tr itemprop="spatial" itemscope="itemscope" itemtype="http://schema.org/Country">',"",$myLine);

$myLine = str_replace('<tr valign="top" itemprop="producer" itemscope="itemscope" itemtype="http://schema.org/Person">',"",$myLine);

$myLine = str_replace('<td class="value links">',"",$myLine);

$myLine = str_replace('<td class="label">',"Label: ",$myLine);

$myLine = str_replace('<td class="value">',"Valor: ",$myLine);

$myLine = str_replace('<td class="value" itemprop="temporal">',"Valor: ",$myLine);

$myLine = str_replace('<td class="value"  itemprop="name">',"Valor: ",$myLine);

$myLine = str_replace('<td class="value" itemprop="name" >',"Valor: ",$myLine);

$myLine = str_replace('<td class="value" >',"Valor: ",$myLine);

$myLine = str_replace('</td>',"",$myLine);

$myLine = preg_replace(array("/\t/", "/\s{2,}/", "/\n/", "/\r/"), array("", " ", " ", " "), $myLine);

echo $myLine;

?>

Browser other questions tagged

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