<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php
error_reporting(E_ALL & ~ E_NOTICE);
$html = file_get_contents("http://www.agropan.coop.br/cotac.htm");
$DOM = new DOMDocument();
libxml_use_internal_errors(true);
$DOM->loadHTML($html);
libxml_clear_errors();
$finder = new DomXPath($DOM);
$classname = 'MsoNormal';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
$result=$result.$node->nodeValue."***";
}
$result = preg_replace(array("/\t/", "/\s{2,}/", "/\n/"), array("", " ", " "), $result);
$partes = explode('***',$result);
$cotacoes=$partes[0];
$cotacoes = trim(preg_replace('/[\r\n]+/', '', $cotacoes));
$cotacoes = str_replace("COTAÇÕES", "", $cotacoes);
echo $cotacoes;
?>
Other ways to avoid errors due to invalid entities "Tag o:p invalid in Entity".
1: Making replace of these invalid entities:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php
error_reporting(E_ALL & ~ E_NOTICE);
$html = file_get_contents("http://www.agropan.coop.br/cotac.htm");
$search = array("<o:p>", "</o:p>");
$replace = array("", "","<div>");
$html = str_replace($search, $replace, $html);
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'MsoNormal';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
$result=$result.$node->nodeValue."***";
}
$result = preg_replace(array("/\t/", "/\s{2,}/", "/\n/"), array("", " ", " "), $result);
$partes = explode('***',$result);
$cotacoes=$partes[0];
$cotacoes = trim(preg_replace('/[\r\n]+/', '', $cotacoes));
$cotacoes = str_replace("COTAÇÕES", "", $cotacoes);
echo $cotacoes;
?>
2: Using a @ in $DOM->loadHTML($html); `@$DOM->loadHTML($html);
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php
error_reporting(E_ALL & ~ E_NOTICE);
$html = file_get_contents("http://www.agropan.coop.br/cotac.htm");
$DOM = new DOMDocument();
@$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'MsoNormal';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
$result=$result.$node->nodeValue."***";
}
$result = preg_replace(array("/\t/", "/\s{2,}/", "/\n/"), array("", " ", " "), $result);
$partes = explode('***',$result);
$cotacoes=$partes[0];
$cotacoes = trim(preg_replace('/[\r\n]+/', '', $cotacoes));
$cotacoes = str_replace("COTAÇÕES", "", $cotacoes);
echo $cotacoes;
?>
By id just replace
$classname = 'MsoNormal';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
for
$id = 'MsoNormal';
$nodes = $finder->query("//*[contains(@id, '$id')]");
Oops, good night, can you please explain to me what you intend to do? You have a website that you need to get only what you have inside a div that has an "x" class that’s it?
– Marcus Rodrigues
exactly friend, I need to search the content of another site of a certain tag, through Classname, I put this code above to exemplify
– Wlader Murilo Alexandro
Got it, I’ve done it, you’ll use Curl, I’m not in front of a computer now, later if I haven’t solved, I length in putting an example here!
– Marcus Rodrigues
I edited to avoid errors. see if it works that way.
– user60252
The DOM is more complete and more complicated than, in your case, killing an ant with a cannonball. Experiment with the Simplexmlelement
– Bruno Augusto