Page 1 of 1

Html page to array

Posted: Wed Jul 23, 2008 3:43 pm
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

Re: Html page to array

Posted: Wed Jul 23, 2008 4:00 pm
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.

Re: Html page to array

Posted: Wed Jul 23, 2008 4:01 pm
by JB4
Thanks.

And then how do I get the specific line from that?

Re: Html page to array

Posted: Wed Jul 23, 2008 5:05 pm
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];

Re: Html page to array

Posted: Wed Jul 23, 2008 7:39 pm
by JB4
manixrock wrote:

Code: Select all

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

Re: Html page to array

Posted: Wed Jul 23, 2008 8:02 pm
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];

Re: Html page to array

Posted: Wed Jul 23, 2008 8:10 pm
by JB4
ok, I figured either it was a typo, or you start on line 0.