Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get first and second element with fopen PHP

Tags:

php

I want to use 'fopen' for opening a file and finding a value that is repeated twice in one html file with other functions.

For example , in this page :

<h1> test </h1>
<h1> test </h1>

If i want to get h1 element, i can just get the first one. And the secound one is not accessable . Now , how can we access first and second and maybe next elements?

like image 826
Pap Avatar asked Dec 09 '25 05:12

Pap


1 Answers

Simply use file_get_contents() of that file , get those HTML content and pass it to the DOM's loadHTML() and you can finally cycle it up using a loop as shown in the code.

foreach ($dom->getElementsByTagName('h1') as $htag) {
        echo $htag->nodeValue;
}

Demo

like image 135
Shankar Narayana Damodaran Avatar answered Dec 11 '25 18:12

Shankar Narayana Damodaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!