0

quick question:

So I've written a bunch of crawling code, but one of the websites I'm crawling didn't include line breaks between tags. Since I've already written a bunch of code, I threw in a quick hack with preg_replace and continued from where I left off. Problem is, fopen() doesn't work on strings...

$string = file_get_contents($url);
$string = preg_replace("/>(^\n|\n+)?</", ">\n<", $string);
$file = fopen($string, 'r');
while(($buffer = fgets($file)) != false) { ... }

So, without rewriting my loop, how do I approach this?

Thanks for the help!

Rob

4

1 回答 1

0

It seems you're looking to iterate over each line of the fetched page. You can do this after you've pulled it into a string by using the explode() function to break the string into an array at the line breaks:

foreach (explode("\n", $string) as $line) {
    ....
}
于 2013-03-30T02:35:51.220 回答