Here I am echoing out class elements from a webpage that I require (= 'titre'), but how do I echo out their value? Can't seem to make it work.
$html = file_get_contents('http://www.spoofedwebpage.int/notice/search/wanted');
$dom = new DOMDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$divs = $dom->getElementsByTagName('div'); // Sample to get div element
$spans = $dom->getElementsByTagName('span'); // Sample to get div element
$i = 0;
while($span = $spans->item($i++))
{
$class_node = $span->attributes->getNamedItem('class');
if($class_node)
{
if($class_node->value == "titre")
{
echo "Class is : " . $span->attributes->getNamedItem('class')->value .
"<br>";
}
}
}
Class attributes are a little special, because they are token lists (several class names seperated by whitespace), However I suggest you use Xpath to fetch the nodes:
$html = <<<'HTML'
<div class="titre foo">abc</div>
<span class="titre bar">def</span>
<div class="bar">hij</div>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXpath($dom);
$expression = '//*[(self::div or self::span) and contains(concat(" ", normalize-space(@class), " "), " titre ")]';
foreach ($xpath->evaluate($expression) as $node) {
echo "Text is: ", $node->nodeValue, "
";
}
Output: https://eval.in/162248
Text is: abc
Text is: def
About the expression:
Select any element in the document ...
//*
... if it is a div
or span
...
//*[(self::div or self::span)]
... and the class
attribute contains a class name titre
//*[ (self::div or self::span) and contains(concat(" ", normalize-space(@class), " "), " titre ") ]
normalize-space()
takes a string and converts all whitespace groups into single spaces. It strips whitespaces from the start and end of the string too. concat
is used to add a single space to the start and the end of the normalized string. The result is a string of class names like SPACE CLASS_ONE SPACE CLASS_TWO SPACE. This allows it to look for a substring like SPACE CLASS_ONE SPACE.
btw Are you sure that you're looking for titre
and not title
?
HINT The original answer selected the class attributes, see the history if you're interested in it.