Can someone please hekp me develope a script that will read from a .txt file ( ex: songs.txt ) and strip out all the songs listed in the file minus the duplicates ... as there will be 2 or 3 of each one listed in the .txt file ... example

did it feel like love
did it feel like love
did it feel like love
did it feel like love
time for loving
time for loving
time for loving
time for loving
when i grow up
when i grow up
when i grow up

ETC ETC I am not sure on how many copies of each title will appear in the file or home many different song titles there will be in total but what I want to do ( learn ) is take only one copy of each song name make sure its not a duplicate and then write one copy of each file to a new text file ...

Can this be done ?

Thanks In Advance

Recommended Answers

All 10 Replies

Any reason not to use the previous thread you started?

Will Gresham, You are right! he/she must give answer....

Why you are not using the previous thread...

Me and Atli are fighting at there for a good code.....
and you made a new thread ? It's not fare... Just come to old thread you have created, to get the answer !

try this

<?
$songs = file('songs.txt');
var_dump($songs);
echo "<br /><br /><h3>result</h3>";
$filtered_songs	=	array_unique($songs);
var_dump($filtered_songs);
?>

Can someone please hekp me develope a script that will read from a .txt file ( ex: songs.txt ) and strip out all the songs listed in the file minus the duplicates ... as there will be 2 or 3 of each one listed in the .txt file ... example

did it feel like love
did it feel like love
did it feel like love
did it feel like love
time for loving
time for loving
time for loving
time for loving
when i grow up
when i grow up
when i grow up

ETC ETC I am not sure on how many copies of each title will appear in the file or home many different song titles there will be in total but what I want to do ( learn ) is take only one copy of each song name make sure its not a duplicate and then write one copy of each file to a new text file ...

Can this be done ?

Thanks In Advance

<?
$songs = file('songs.txt');
var_dump($songs);
echo "<br /><br /><h3>result</h3>";
$filtered_songs	=	array_unique($songs);
var_dump($filtered_songs);
?>

Firstly the reason for doing so was this is a DIFFERENT request then the one I first created for a totally different script then what I asked for in my other post ... and secondly I tried to log back in under my other accound name for that thread and the system logs me in but then back out again for some strange reason ?

In my last script request I asked how to strip a time from a txt file that has a time in ( ) in this post I ask how to read a text file with multiple song names in it without anything to do with the ( ) and strip out only single copies of the song titles no matter how many duplicates there are ... that is the reason for a different post ...

or is that NOT the right thing to do ? I am new here so I dont know all the rules or why I would have been locked out of my account ?

and secondly I tried to log back in under my other accound name for that thread and the system logs me in but then back out again for some strange reason ?

I would recommend inquiring about this problem in Community Feedback.

That does exactly what I am trying to do ... except for a few small things

1) I dont want to print the current contents to the screen
2) I dont want to print the results to a screen either ( LOL )

strange I know

But what I want to do is have it read the file ( which you did )
then have it remove all the doubles ( which you did )

but then I want it to output the corrected version to another text file ( dont care about the name of that file ) and have a <br> after each song title so they are all on seperate lines not sharing the same line ....

Please let me know if that can be done ? As your current script does the sort EXCELLENT but I need to also have it to the things I mentioned above ...

Thank you so much in advance for al lyour help

Midnite

So does anyone have an answer for me ? Please let me know I would be most greatful !!!

<?php
$songs = file('songs.txt');
var_dump($songs);
echo "<br /><br /><h3>result</h3>";
$filtered_songs	=	array_unique($songs);
var_dump($filtered_songs);
if(sizeof($filtered_songs)>0)
{
	$content	=	"";
	foreach($filtered_songs as $song)
	{
		$content	=	$content.$song;
	}

}

$myFile = "newfile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);

?>

arunss TY for all your help that is EXACTLY what I needed now all I need is the same idea but I need a seperate script that will run thru each hour of the day

EX:
1am 2am 3am 4am 4am 6am 7am 8am 9am 10am 11am 12pm ... 12am

and take each hours file ( say for ex 10am.txt ) and add all the songs in each file to one file for that day ... ( ex playlistsun.txt for sunday , or playlistmon.txt for monday ETC ETC ...

so in effect it needs to open each hours file and append it to one file in order from 12am to 11pm for each day based on the servers day and time ... and dont worry about telling it when to run I want to do this once a night thru a cron job ...

Thank you so much if you can help ... or anyone else ...

please have it name the file playlistsun.txt or playlistmon.txt based on the day it is on the server ...

Thanks
Midnite

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.