userrr3
userrr3

Reputation: 483

PHP strstr() returns only one line

first of all: I really love this site and I think this is the best forum for programming :)

Now to my problem, which I try to display using code and comments:

    $file = fopen ($URL, "r");
    // $URL is a string set before, which is correct
    // Also, I got the page-owners permission to acquire the page like that
    if (!$file) 
    {
        echo "<p>Could not open file.\n";
        exit;
    }
    while (!feof ($file)) 
    {
        $buffer = fgets($file);
        $buffer= strstr($buffer, "Montag</b>"); 
        // If I don't use this line, the whole page gets displayed...
        // If I use this line, only the first line after the needle gets displayed
        echo $buffer;
    }
    fclose($file);

So basically, I'm able to display the whole page, or one line after the needle, but not everything after the needle....

I tried to find a solution using the PHP Reference, the Stackoverflow Search engine and of course google, but I couldn't find a solution, thanks for everybody willing to help me.

Greetings userrr3

Upvotes: 1

Views: 991

Answers (1)

Treffynnon
Treffynnon

Reputation: 21553

Extracting text from the file

You are only grabbing one line at a time from the file using fgets() DOCs if you want the whole file then use file_get_contents() DOCs instead:

$file = file_get_contents($URL);
$buffer= strstr($file, "Montag</b>"); 
// If I don't use this line, the whole page gets displayed...
// If I use this line, only the first line after the needle gets displayed
echo $buffer;

Grabbing your text

This can be achieved using PHPs substr() DOCs function combined with strpos() DOCs:

$buffer = substr($buffer, strpos($buffer, 'Montag</b>'));

This will grab all the text after the first occurrence of the needle Montag</b>.

Putting it all together

$file = file_get_contents($URL);
$buffer = substr($file, strpos($buffer, 'Montag</b>'));
echo $buffer;

Upvotes: 2

Related Questions