Jump to content

[SOLVED] Help with modifying & displaying text file


thomasgrant

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 - ")

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.