LackOfLuck Posted July 15, 2009 Share Posted July 15, 2009 hi I'm pretty new to php so I think this would be an easy question for most of you :x. So I have a huge text file.All the words are separated by comma. I want every word on this text to be putted in new line. There are a lot of words so doing it manually wouldn't be worth the effort. First I searched for a string function but couldn't find anything . maybe this could be solved by an array function. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/ Share on other sites More sharing options...
genericnumber1 Posted July 15, 2009 Share Posted July 15, 2009 I'd probably use stream_get_line to get each word individually. Or, if the file is small enough that it can fit in memory all at once, you could use the more simple solution of file_get_contents along with explode. Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875605 Share on other sites More sharing options...
LackOfLuck Posted July 15, 2009 Author Share Posted July 15, 2009 I'd probably use stream_get_line to get each word individually. Or, if the file is small enough that it can fit in memory all at once, you could use the more simple solution of file_get_contents along with explode. hi thanks for your reply but can you give me an example on how you would use this? for example the text is: text12323, tex2131231,textsat2323,text321131 ... and so on what should my code look like? sorry if my questions are stupid but again i'm really new to php and I'm sorry if I annoy you Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875609 Share on other sites More sharing options...
genericnumber1 Posted July 15, 2009 Share Posted July 15, 2009 Are you trying to create a new file with this content or are you just trying to output it? Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875616 Share on other sites More sharing options...
LackOfLuck Posted July 15, 2009 Author Share Posted July 15, 2009 Are you trying to create a new file with this content or are you just trying to output it? Just trying to output Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875617 Share on other sites More sharing options...
genericnumber1 Posted July 15, 2009 Share Posted July 15, 2009 The safer way which doesn't load the whole file into memory would look something like this... <?php $maxLineLength = 1024; // Longest possible 'word' $oldLineDelimiter = ","; $newLineDelimiter = "\n"; // <br /> if new lines are to be displayed in the browser // Open the file $fileHandle = fopen('/path/to/my/file.txt', 'r'); // Or whatever file name you need if($fileHandle) { // Read each line and output them one-by-one while(!feof($fileHandle)) { $word = stream_get_line($fileHandle, $maxLineLength, $oldLineDelimiter); echo $word . $newLineDelimiter; } // Clean up fclose($fileHandle); } else { echo 'Unable to open file.'; } ?> I didn't test it, so if there's a bug, you'll have to fix it! Don't just copy the code, try to figure out how it works and why I do things the way I do. Feel free to ask any questions, just know I may be asleep by the time you ask them, but I'd be happy to answer them tomorrow. Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875622 Share on other sites More sharing options...
aschk Posted July 15, 2009 Share Posted July 15, 2009 I'm guessing your file looks a bit like this: text, text, text, text, text, text, text, text, text, text, text, text, etc... So you just want to output each of those comma separated values? If so try the following: <?php $filename = "<the name of the data file.txt>"; $data = file_get_contents($filename); $data = explode(",", $data); if(count($data)){ echo "<ul>"; foreach($data as $line){ echo "<li>{$line}</li>"; } echo "</ul>"; } ?> The above piece of code will output the data as an html list. n.b. make sure to change the code above to include the name of your data file (1st line). Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875635 Share on other sites More sharing options...
genericnumber1 Posted July 15, 2009 Share Posted July 15, 2009 Use aschk's solution only if the file is small enough to all fit in memory at one time. I assumed because he called it a "huge text file" that it couldn't fit in memory at once. Aschk's solution is more simple and faster though if you have the memory to spare. Link to comment https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.