Search code examples
phphtmlpre

How do I get the value of a <pre> tag with no ID?


I have the following code set up from an example:

<?php
$url = 'http://somedomain/something';
$content = file_get_contents($url);
$first_step = explode( '<div id="somediv">' , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];

?>

The problem here is that the website from which I'm trying to fetch the value of the pre tag has no ID:

<pre>some content</pre>

I've also tried this but no success so far:

<?php
$url = 'http://somedomain/something';
$content = file_get_contents($url);
$first_step = explode( '<script>document.getElementsByTagName("pre")' , $content );
$second_step = explode("</script>" , $first_step[1] );

echo $second_step[0];

?>

Basically, I'm trying to fetch a value from a domain which is wrapped by a pre tag with no additional identifiers. Any help appreciated!


Solution

  • PHP ships with a pretty decent document parser:

    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://somedomain/something');
    
    foreach ($dom->getElementsByTagName('pre') as $node) {
        // do stuff with $node
        echo $node->nodeValue, "\n";
    }
    

    See also: DOMDocument