devWhiz Posted April 28, 2011 Share Posted April 28, 2011 <?php $file = 'test.txt'; $lastfm = file_get_contents('http://www.last.fm/group/Rishloo/members'); preg_match_all('/id="r4_([\d]+)">/', $lastfm, $matches); file_put_contents($file, $matches[1]); sleep(100000); ?> That puts the array in a file but I need each value on a new line but it bunches it all up.. this just puts "Array" in file <?php $file = 'test.txt'; $lastfm = file_get_contents('http://www.last.fm/group/Rishloo/members'); preg_match_all('/id="r4_([\d]+)">/', $lastfm, $matches); file_put_contents($file, $matches[1]."\n"); sleep(100000); ?> what do I need to do to print all of the values on a new line? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/ Share on other sites More sharing options...
wildteen88 Posted April 28, 2011 Share Posted April 28, 2011 You need to convert the array into a string. For this you can use implode file_put_contents($file, implode("\n", $matches[1])); Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207778 Share on other sites More sharing options...
devWhiz Posted April 28, 2011 Author Share Posted April 28, 2011 Wildteen your awesome!! helped me with alot of stuff today.. one more question so file_put_contents($file, implode("\n", "blah".$matches[1])); writes the matches each on a new line... how would I go about putting something in front of the id like this $ID[] = 1234578; $ID[] = 1234578; $ID[] = 1234578; $ID[] = 1234578; $ID[] = 1234578; $ID[] = 1234578; instead of 1234578 1234578 1234578 1234578 1234578 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207862 Share on other sites More sharing options...
wildteen88 Posted April 28, 2011 Share Posted April 28, 2011 If you're planning on adding all ids into an array again then you can just open the file with file. That function will load each line into an array. So you won't need to add $ID[] = at the start of each line. Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207867 Share on other sites More sharing options...
devWhiz Posted April 28, 2011 Author Share Posted April 28, 2011 sweet that works! now one last thing and ill be done.. how would I add a \n line break at the beginning of each id Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207923 Share on other sites More sharing options...
devWhiz Posted April 28, 2011 Author Share Posted April 28, 2011 Cant edit above post, but this is the code I have.. <?php $file = 'Lastfm IDs.txt'; $ch = curl_init(); for($cwb=1; $cwb!=399; $cwb++) { curl_setopt($ch, CURLOPT_URL, "http://www.last.fm/group/i'm+not+anti-social%2C+i+just+enjoy+my+music./members?memberspage=".$cwb); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); preg_match_all('/id="r4_([\d]+)">/', curl_exec($ch), $matches); file_put_contents($file, implode($matches[1], "\n"), FILE_APPEND); echo "#".$cwb." pages with user ids loaded...\n"; } curl_close($ch); sleep(100000); ?> now,, everytime it does a loop it loads a different members page.., every time it loads a new one and writes the ids from that page.. it looks like this in the id file 14423441 32839962 36683385 3670648838089858 11899884 31379239 31517343 14856674 when it should be 14423441 32839962 36683385 36706488 38089858 11899884 31379239 31517343 14856674 Thanks for the help man Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207936 Share on other sites More sharing options...
QuickOldCar Posted April 28, 2011 Share Posted April 28, 2011 Change to use this line, it'll add the extra return. file_put_contents($file, implode($matches[1], "\n")."\n", FILE_APPEND); May I ask in what way these id's will be used? I'm curious. Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207949 Share on other sites More sharing options...
devWhiz Posted April 29, 2011 Author Share Posted April 29, 2011 shit man so simple! how did I not think of that.. prob cuz I tried it without implode and it just wrote "Array" to the file Lol, thanks man I appreciate it!! And last.fm is a site where people can discover new music based on the music you scrobble on yor computer with the last fm scrobbler, so it records everything you listen to and recommend similar artists to broaden your taste and such in music.. if that makes sense well this script loads members pages from group on the site that people put together so people can talk about music and such, well the script loads all of the ids of members in the group.. and writes to a text file.. Later on I am going to make a script that will request friendship with that list of ids.. I will have a couple hundred thousand by time Im done Thanks for the help, Im sure I will need some help when I go to write the script to add the friends, I usually get stuck where it is always a simple solution.. Lol Quote Link to comment https://forums.phpfreaks.com/topic/235012-writing-array-to-a-file/#findComment-1207962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.