Html page to array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JB4
Forum Commoner
Posts: 33
Joined: Tue Jul 22, 2008 6:07 pm
Location: Utah

Html page to array

Post by JB4 »

Ok I have 2 options that I need to figure one or the other out

I have $URL set to a link (ex. http://www.google.com)

Option 1:
What I need/want is to set each line of that page to a line of the array. Then i need it to select ONLY line #179 and save it as $179

Option 2:
just cut everything but line 179 of the html file. and save it as $line
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Html page to array

Post by alex.barylski »

Code: Select all

$buff = file_get_contents('some/html/page.html');
$lines = explode("\n", $buff);
Alternatively:

Code: Select all

$lines = file('some/html/page.html');
The latter is probalby best as I would assume it handles the differences between systems and CRLF caveats.
JB4
Forum Commoner
Posts: 33
Joined: Tue Jul 22, 2008 6:07 pm
Location: Utah

Re: Html page to array

Post by JB4 »

Thanks.

And then how do I get the specific line from that?
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Html page to array

Post by manixrock »

$lines is now a 0 based (meaning the first line has index 0, the second line has index 1, and so on) array containing all the lines of the source code of the web page in question.

To access the 179rd line use this:

Code: Select all

$line179 = $lines[178];
JB4
Forum Commoner
Posts: 33
Joined: Tue Jul 22, 2008 6:07 pm
Location: Utah

Re: Html page to array

Post by JB4 »

manixrock wrote:

Code: Select all

$line179 = $lines[178];
$lines[178] or is that supposed to be 179?
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Html page to array

Post by manixrock »

JB4 wrote:
manixrock wrote:

Code: Select all

$line179 = $lines[178];
$lines[178] or is that supposed to be 179?

In a 0-based array the 1st line would have index 0, 2nd line index 1, ..., 179th line index 178.

So you get the 179th line with $lines[178];
JB4
Forum Commoner
Posts: 33
Joined: Tue Jul 22, 2008 6:07 pm
Location: Utah

Re: Html page to array

Post by JB4 »

ok, I figured either it was a typo, or you start on line 0.
Post Reply