Codice PHP:
<?php
$html = file_get_contents('http://google.it/');
$tag = "dt";
$tag0 = "dd";
$M = new html();
$M ->htmls($tag, $tag0, $tag1, $html);
?>
<?php
class html{
function htmls($tag, $tag0, $tag1, $html)
{
/*** a new dom object ***/
$dom = new domDocument;
$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = true;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
$item0 = $dom->getElementsByTagname($tag0);
$item1 = $dom->getElementById('yw-cond');
$out = "";
$out0 = "";
/*** the array to return ***/
foreach ($content as $item)
{
$out.= $item->nodeValue." ";
}
foreach ($item0 as $item0)
{
$out0.= $item0->nodeValue." ";
}
$m=explode(" ",$out);
$m0=explode(" ",$out0);
$testo = $item1->nodeValue."___".$m[0]." ".$m0[0].$m0[1]."___".$m[1]." ".$m0[2].$m0[3]." ".$m0[4]." ".$m0[5]."___".$m[2]." ".$m0[6].$m0[7]."___".$m[3]." ".$m0[8].$m0[9]."___".$m[4]." ".$m[5]." ".$m[6]." ".$m0[10].$m0[11]."___".$m[7]." ".$m0[12]." ".$m0[13]." ".$m0[14]."___".$m[8]." ".$m0[15].$m0[16]."___".$m[9]." ".$m0[17].$m0[18];
echo $testo;
?>
A questo codice che gia prendere elementi in base al loro tag e id dovrei aggiungere anche tramite class.
Ho gia provato ad adattare al php questi metodi trovati:
#1
Codice PHP:
--------------
tedd.html
--------------
<html>
<div>
sadfasdf
</div>
<h1>hello</h1>
<p class="question">
Who is Roger Rabbit?
</p>
<h2>more stuff</h2>
<p class="question">
Who is Roger Rabbit?
</p>
</html>
---------------------
transform.php
---------------------
<?php
// here is where you load a single file or change to iterate over a
// directory of files
$oDomDoc = DOMDocument::loadHTMLFile('./tedd.html');
// here is where you search for the question sections of each file
$oDomXpath = new DOMXPath($oDomDoc);
$oNodeList = $oDomXpath->query("//p[@class='question']");
// here is where you extract the question sections of each file
foreach($oNodeList as $oDomNode)
var_dump($oDomNode->nodeValue);
2#
Codice PHP:
$htmlSource = '<div class="uniqueClass"><span style="text-align: center;" /></div>';
$doc = new DOMDocument;
// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;
$doc->loadHTML($htmlSource);
$xpath = new DOMXPath($doc);
// We starts from the root element
$query = '//div[@class= uniqueClass]/span';
$entries = $xpath->query($query);
$spanStyle = $entries->current()->getAttribute('style')
Non so se sbaglio io o c'è un problema con determinate funzioni non gestite da altervista.
Se mi dite come dovrei fare (in base al mio script,così evito di rifarlo) a ottenere ocme risultato il value di un tag con un class specifico.
-
Ho risolto,rivedere tutti e 3 gli script insieme nel post mi ha aperto gli occhi..
Codice PHP:
$xpath = new DOMXPath($dom);
// We starts from the root element
$query = "//div[@class='valore class']";
$entries = $xpath->query($query);
foreach($entries as $div){
$divid.= $div->nodeValue.";";
}
echo $divid;