We have this simple HTML page (for test!) :
<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>
So, i need a very simple php code to crawl. The thing i want to be crawled, is that i want to have : "one","two","three" into a php array.I need to crawl everything that is into "my" class. And i don't want to have the other classes.
try this you can use xpath to get your result
$html = '<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="my"]');
foreach ($tags as $tag) {
$node_value = trim($tag->nodeValue);
echo $node_value."<br/>";
}
You should make use of the DOMDocument Class
<?php
$html='<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>';
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $tag) {
if ($tag->getAttribute('class') === 'my') {
echo $tag->nodeValue; // to get the content in between of tags...
}
}
OUTPUT :
One Two Three
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With