thomasgrant Posted January 19, 2008 Share Posted January 19, 2008 I've done a bit of reading today on examples in working with text files. I get the basics, and was hoping I wouldn't have to reach out for help. Alas, here I am looking for assistance. What I am looking to do is read and change the contents of a text file. Then, display the results to the user. The text file in this scenario will be an M3U formatted playlist. If you're unfamiliar with the format, I will give an example. #EXTM3U #EXTINF:123,Sample title C:\Documents and Settings\I\My Music\Sample.mp3 #EXTINF:321,Example title C:\Documents and Settings\I\My Music\Greatest Hits\Example.ogg Using the above as an example, what I am looking to do is end up with the following output: 01 - Sample.mp3 02 - Example.ogg Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/ Share on other sites More sharing options...
thomasgrant Posted January 19, 2008 Author Share Posted January 19, 2008 I wanted to modify the original post to add the following, but was unable to. In essance, I will always wanna remove the FIRST row of the file. And then each additional EVEN row. Next, the path to the filename and extension. Lastly, add track numbers. It seems like it would be mildly easy up to removing the path to each file as they could be different. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-443785 Share on other sites More sharing options...
pkSML Posted January 20, 2008 Share Posted January 20, 2008 More rather, you want to retrieve every line that does not begin with the pound sign. so... Read the file into an array - the file() function Make a foreach loop that reads the array (Put a variable in the foreach loop that will keep track of the track number) Put a conditional if statement that asks if the line begins with # If not, display the track number variable, increment it, and use pathinfo() to extract the filename for display Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-443933 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 Just to add to the comment above you can use substr() to get the filename in one: <?php $filename = substr($file, strrpos($file, '\\')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-443940 Share on other sites More sharing options...
thomasgrant Posted January 20, 2008 Author Share Posted January 20, 2008 I guess I was in a hurry when I posted this. I thought more about this long after I posted it and realized with the request I made it would be harder than it had to be. Let me re-clarify what I'd like. 1. Always remove first line of file 2. Always remove lines WITHOUT '#' at the beginning 2. Add sequential track numbers before each line. Thanks for the input thus far, any further assistance would be awesome. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444034 Share on other sites More sharing options...
thomasgrant Posted January 20, 2008 Author Share Posted January 20, 2008 Jeez, I don't get much time after posting to modify. An update to my requirements: 1. Always remove first line of file 2. Always remove lines WITHOUT '#' at the beginning 3. Remove the first 12 character from each remaining line (ie. "#EXTINF:321,") 4. Add sequential track numbers and formatting before each line. (ie "01 - ") Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444036 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 If you remove the lines without hash signs in front then you won't be able to get to the filename of the file and so can't do something like '01 - Sample.mp3' because the line with Sample.mp3 in would be gone... Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444040 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 1. Always remove first line of file 2. Always remove lines WITHOUT '#' at the beginning 3. Remove the first 12 character from each remaining line (ie. "#EXTINF:321,") 4. Add sequential track numbers and formatting before each line. (ie "01 - ") consider: 1. If starts "#EXTINF:" pull needed info 2. if not start "#" pull needed info 3. Add track number Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444044 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 What do you need the Sample Title for? You don't seem to be using it in your formatting anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444054 Share on other sites More sharing options...
thomasgrant Posted January 20, 2008 Author Share Posted January 20, 2008 If you remove the lines without hash signs in front then you won't be able to get to the filename of the file and so can't do something like '01 - Sample.mp3' because the line with Sample.mp3 in would be gone... What do you need the Sample Title for? You don't seem to be using it in your formatting anywhere. Guys, I apologize for any confusion. That was a bad example to use. I pulled that from the wiki page I linked. Here is a real world example, that is coming from my source: #EXTM3U #EXTINF:207,David Guetta - Sexy C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\08_-_david_guetta_-_sexy.mp3 #EXTINF:194,David Guetta - Just A Little More Love (Radio Edit) C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\01_-_david_guetta_-_just_a_little_more_love_(radio_edit).mp3 #EXTINF:199,David Guetta - People Come People Go C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\07_-_david_guetta_-_people_come_people_go.mp3 #EXTINF:358,David Guetta - You Are The Music C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\12_-_david_guetta_-_you_are_the_music.mp3 #EXTINF:344,David Guetta - Give Me Something C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\03_-_david_guetta_-_give_me_something.mp3 #EXTINF:293,David Guetta - Can't U Feel The Change C:\Download\[ music ]\[ albums ]\David Guetta - Just A Little More Love (2002)\05_-_david_guetta_-_can't_u_feel_the_change.mp3 As you can see the lines that begin with "#EXTINF:" do include the song artist and title. This is pulled from the ID3 tags contained within each file. On top of that, it's most likely going to be formatted in correct case if the filenames are not. I hope this clears things up, and I wish I had presented this better from the first post forward. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444572 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 <?php $lines = file('playlist.m3u'); $count = 0; foreach($lines as $line) { if($line[0] != '#') continue; if(trim($line) == '#EXTM3U') continue; $count++; $fcount = (string) $count; if($count < 10) $fcount = '0' . $fcount; echo "$fcount - " . substr($line, 12) . "\n"; } ?> That should be most of the logic, the output can be changed. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-444585 Share on other sites More sharing options...
thomasgrant Posted January 21, 2008 Author Share Posted January 21, 2008 That should be most of the logic, the output can be changed. That worked wonderfully. And I thank you very much. I'm now adapting this concept into copy/pasting a playlist into a textarea as opposed to uploading a file. For this I replaced: $lines = file('playlist.m3u'); With: $lines = explode('',$m3u); Where $m3u is equal to the string that was sent from the form. I understand how this function works, however I do not know what string delimiter to use inbetween the single quotes. I know that when I've displayed this string on screen before, it does not see the carriage returns as it were from the original text file. Does this make sense? Any input on how to recognize the carriage returns once the information is copied into the textarea? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-445185 Share on other sites More sharing options...
Fyorl Posted January 21, 2008 Share Posted January 21, 2008 If you display the text that's come directly from a textarea then you won't see any newlines because HTML ignores carriage returns. If you wanted to see the text as a text editor would display it then you need to put the output within '<pre>' tags. Anyway, to solve your you'll need to use: $lines = explode("\n", $m3u); As PHP interprets \n as the newline character when it's put inside double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-445285 Share on other sites More sharing options...
thomasgrant Posted January 21, 2008 Author Share Posted January 21, 2008 If you display the text that's come directly from a textarea then you won't see any newlines because HTML ignores carriage returns. If you wanted to see the text as a text editor would display it then you need to put the output within '<pre>' tags. Anyway, to solve your you'll need to use: $lines = explode("\n", $m3u); As PHP interprets \n as the newline character when it's put inside double quotes. Before I even got your reply I had tried: $lines = explode('\n', $m3u); It's nice to know I was close, just happen to use the incorrect quotes. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-445319 Share on other sites More sharing options...
Fyorl Posted January 21, 2008 Share Posted January 21, 2008 Woah, sorry, I didn't realise my reply was so badly formatted, turns out it parses 'pre' tags. Quote Link to comment https://forums.phpfreaks.com/topic/86834-solved-help-with-modifying-displaying-text-file/#findComment-445354 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.