michaelmcgurk michaelmcgurk - 3 years ago 100
PHP Question

Get specific line of web page using file_get_contents

I have a file

test.txt
over 4 lines:

This
Is
A
Test


I have a PHP script which when I run grabs the contents of this file and displays for me:

<?php
$file = file_get_contents('https://www.mywebsite.com/test.txt');
echo $file;
?>


Is there any way to get a specific line from the
test.txt
file?

Answer Source

you could explode() with new line, like:

$string = file_get_contents('https://www.mywebsite.com/test.txt');
$stringArr = explode("\n", $string);
echo $stringArr[2]; //get the third line

or use file() which Reads entire file into an array, like:

$lines = file('https://www.mywebsite.com/test.txt');
echo $lines[2];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download