Andreas
Andreas

Reputation: 23968

Dom document parse

I have never used dom document and need some help.
I have tried to look at other threads and other tutorials but I can't see where I'm doing wrong.

This is the page: https://aro.lfv.se/Links/Link/ViewLink?TorLinkId=310&type=MET
Here is a smal part of the html;

        <h1 class="tor-link-header">&#214;versikt</h1>
            <pre class="linkTextNormal">&#214;VERSIKT F&#214;R OMR&#197;DE E UTF&#196;RDAD 040753
G&#196;LLANDE DEN 4 MAJ 2017 MELLAN 08 OCH 16 UTC


V&#228;der&#246;versikt
Se v&#228;der&#246;versikt f&#246;r omr&#229;de A+B

Sikt under 5 kilometer eller molnbas under 1000 fot
V&#228;ntas inte f&#246;rekomma under perioden.

M&#229;ttlig eller sv&#229;r isbildning
V&#228;ntas inte f&#246;rekomma under perioden

M&#229;ttlig eller sv&#229;r turbulens
08-16UTC: I hela omr&#229;det</pre>
        <h1 class="tor-link-header">Hela Omr&#229;de E</h1>
            <pre class="linkTextNormal">PROGNOS F&#214;R OMR&#197;DE J UTF&#196;RDAD 040753
G&#196;LLANDE DEN 4 MAJ 2017 MELLAN 08 OCH 16 UTC

I have found out that in the whole html there is only two <pre> tags, and both of them is interesting to me.

I found this code (tweaked it slightly to fit my code) but it does not work.

$doc = new DOMDocument();
$doc->load( $URL_LHP );

$Parts = $doc->getElementsByTagName( 'pre' );
$Part = $Parts->item(0);
var_dump($Part);

foreach( $Parts as $Part ){
    echo $Part;
}

The var_dump returns NULL, and echo returns nothing. $URL_LHP is the HTML in string format.
If I echo $URL_LHP it will echo the webpage but with "dead" images and no CSS.
So the variable is what I expect.

Can anyone help me with this?

Upvotes: 0

Views: 43

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 99081

This may work for you:

$html = file_get_contents("https://aro.lfv.se/Links/Link/ViewLink?TorLinkId=310&type=MET");
$dom = new DOMDocument();
@$dom->loadHTML($html);
$pres = $dom->getElementsByTagName('pre');

foreach($pres as $pre)
{
    print $pre->nodeValue;
}

Ouput:

ÖVERSIKT FÖR OMRÅDE E UTFÄRDAD 040753
GÄLLANDE DEN 4 MAJ 2017 MELLAN 08 OCH 16 UTC
...

Upvotes: 1

Related Questions