如何使用PHP阅读网页HTML的第n行?

I've seen this question for files, but for some reason they won't ever work with a web page.

I'm trying to use file_get_contents to get the contents of a web page (don't care much about speed, hence I'm not using cURL), and then I want to print a specific line.

Can you please give me the simplest possible way of doing this, as I'm creating an API which fetches specific lines from multiple web pages.

Alternatively, is there a way I can search for and print a line that contains a certain string? For example, a line that starts with "Foo" (if there's only one line containing that).

function readStrLine($str, $n) {
    $lines = explode(PHP_EOL, $str);
    return $lines[$n-1];
}

$file = file_get_contents('http://google.pl');

echo readStrLine($file, 10);

you can explode string by new line, then you got array of lines which start with index 0 (it's first line)

EDIT alternative way with tidy html

function readHtmlLine($html, $n) {
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = false;
    $dom->loadHTML($html);
    $dom->formatOutput = true;
    $lines = explode(PHP_EOL, $dom->saveHTML());
    return $lines[$n-1];
}

$file = file_get_contents('http://google.pl');

echo readHtmlLine($file, 10);

How can I read a specific line of a WEB PAGE? [PHP]

Can you please give me the simplest possible way of doing this, as I'm creating an api which fetches specific lines from multiple web pages.

Alternatively, is there a way I can search for and print a line that contains a certain string?

Sample html file:

file.html

<html>
<head><title>File</title></head>
<body>
    <p>Nancy is my name</p>
    <p>James is my name</p>
    <p>Foo is my name</p>
    <p>Bob is my name</p>
</body>
</html>

simple php function:

function checkFile( $file, $keyword ) {

    // open file for reading
    $handle = @fopen( $file, 'r' );

    // check to make sure handle is valid
    if( $handle ) {

        // traverse file line by line
        while( ($line = fgets($handle)) !== false ) {

            // search for specific keyword no matter what case is used i.e. foo or Foo
            if( stripos($line, $keyword) === false ) {
                // string not found, continue with next iteration
                continue;
            } else {

                // keyword was found

                // close file
                fclose($handle);

                // return line
                return $line;
            }
        }
    }
}

$result = checkFile( 'file.html', 'foo' );

echo $result;

Outputs: <p>Foo is my name</p>