Hey, Just asking for some help with scraping data off a webpage, and storing to a text/csv/xml file, or whatever works best. So this is my basic code that scraped the info i want off a webpage: Does anybody know a php function that allows me to print data to a file? Any help would be much appreciated, thanks.
I hate posts like these. What would you do if you didn't have a forum to go directly to? You would use google. Google PHP File Operations. You will find an easy tutorial on how to create and manipulate text files. Personally I would use a database to store data from a scraper.
Here is a example for scraping site data using php: $string = file_get_contents("http://example.com"); preg_match_all('%<span class="tag">(.*?)</span>%sim', $string, $result); $result = $result[1]; foreach($result as $item){         $item = preg_replace('%<span class="tag-chain-item-span">(.*?)</span>%sim', '$1', $item);         $display .= '<a href="?search='.$item.'">'.$item.'</a><br>';         } echo $display; PHP:
You could also start with an empty string. $out = ""; Then gather up all the elements in the loop $out .= "$element\r\n"; After the loop do a file_put_contents("file.txt", $out);
In fact, yelbom's code will throw an undefined variable error at the first execution of $display .= since $display hasn't been defined yet. (.= doesn't define a variable the way = does.)