jazuly aja
jazuly aja

Reputation: 89

how to print specific tag using curl

how to print text from <p> tag using curl and DOMDocument i try like this but not showing anything.

<?php

$url = "http://www.blablabla.org/dorama/201105455/kimi-wa-petto";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

$dom = new DOMDocument();

@$dom->loadHTML($html);

foreach($dom->getElementsByTagName('p') as $link) {
        echo $link->getAttribute('p');
        echo "<br />";
}
?>

Upvotes: 0

Views: 2747

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

If you just want to print out the text inside the <p> tags, the use $textContent...

foreach($dom->getElementsByTagName('p') as $link) {
        echo $link->textContent;
        echo "<br />";
}

If you want the whole XML, you can output it using saveXML() with the node as the point to output from ...

foreach($dom->getElementsByTagName('p') as $link) {
        echo $dom->saveXML( $link);
        echo "<br />";
}

As this is HTML, you may want to replace the saveXML() call with saveHTML().

Upvotes: 1

Related Questions