otronics Posted August 15, 2007 Share Posted August 15, 2007 I have some php code that reads data from a .txt file and displays it as the Artist and song now playing on a website. The .txt file updates whenever the song changes, and the php code eventually reads the new file and updates accordingly. I want to add some lines in my code that will ignore certain words in the text file and print an alternative message on the website instead of what is playing. The problem is that this .txt file captures everything, such as station ID's and news - obviously all I want displayed on the website is songs now playing and nothing else. So something like, if <text in .txt file> contains the term <list of terms go here> then print <a general message here> Here is the code: <?php // the Current Song Text File $fp = fopen ("http://society.swan.ac.uk/~xtreme/CurrentSong.txt", "r"); // copy the Text file to a variable. $content = fgets( $fp, 60); // Close the file fclose( $fp ); // Cleaning process gets rid of the - and white space and formats in the Song: and line break. $clean1 = str_replace("-", "<br />Song:", "$content"); $clean2 = str_replace(" <br />", "<br />", "$clean1"); $clean3 = str_replace(":", ": ", "$clean2"); // Prints it all out neatly if($content == null) { // If there is no data print an error. echo "<b>Error:</b> Olivers site isn't working!"; } else { //If there are no errors print it all! echo "Artist: "."$clean3"; } echo "<!-- Now Playing Script - copyright Jon Hurlock 2006 -->"; ?> As stated this is copyrighted code! Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/ Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 first please use code tag (#) for code do you have a text file we can see also could you manually clean the file so we can see what you expect the end result to be Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325209 Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 As stated this is copyrighted code! So maybe posting it on a public site isn't too wise. May I ask, are you the author of this script? Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325214 Share on other sites More sharing options...
otronics Posted August 15, 2007 Author Share Posted August 15, 2007 Not my code - I am posting on behalf of our developer who wrote it and enhancing it. Our developer is busy for the next month so I am looking after things. The end result can be seen at our website - not sure if I can post it here but on our website it displays: Artist Songtitle. Not sure what you mean by 'clean the code', sorry. <?php // the Current Song Text File $fp = fopen ("http://society.swan.ac.uk/~xtreme/CurrentSong.txt", "r"); // copy the Text file to a variable. $content = fgets( $fp, 60); // Close the file fclose( $fp ); // Cleaning process gets rid of the - and white space and formats in the Song: and line break. $clean1 = str_replace("-", " Song:", "$content"); $clean2 = str_replace(" ", " ", "$clean1"); $clean3 = str_replace(":", ": ", "$clean2"); // Prints it all out neatly if($content == null) { // If there is no data print an error. echo "Error: Olivers site isn't working!"; } else { //If there are no errors print it all! echo "Artist: "."$clean3"; } echo "<!-- Now Playing Script - copyright Jon Hurlock 2006 -->"; ?> An example text file is simple - it is called CurrentSong.txt and literally has Artist - Songtitle in, ie/ Rolling Stones - Angie. Nothing more, nothing less. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325225 Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 Your going to need to parse the file using something like... preg_match and strip out what you don't want. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325228 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 I refer to this statement The problem is that this .txt file captures everything, such as station ID's and news - obviously all I want displayed on the website is songs now playing and nothing else. OK example Current file Rihanna > Good Girls Gone Bad (2007) > Umbrella Rihanna > Good Girls Gone Bad (2007) > Push Up cleaned file Rihanna > Umbrella Rihanna > Push Up Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325229 Share on other sites More sharing options...
nathanmaxsonadil Posted August 15, 2007 Share Posted August 15, 2007 is this your site? http://www.sucs.org/~xtreme/content/ Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325230 Share on other sites More sharing options...
otronics Posted August 15, 2007 Author Share Posted August 15, 2007 That is our site, or www.xtremeradio.org MadTechie - that's the sort of thing I am after. The only .txt file involved is the one our automation software uses - the php code simply prints to our site. So if this .txt file was to output Xtreme - Sweeper 1 for example, I would like for the code to recognise that and NOT print it to our site, but print a general message such as 'playing more of your best tunes', to cover for it. So an if and else or whatever php uses. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325242 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 OK but i still need to see the data being pulled inn.. any links i can see, basically i can't create a filter without see the data first.. i need to know what your working with Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325248 Share on other sites More sharing options...
otronics Posted August 15, 2007 Author Share Posted August 15, 2007 Actually, best way is just to see the text file that is uploaded here: http://society.swan.ac.uk/~xtreme/CurrentSong.txt It updates every minute and this is the data the php script reads. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325256 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 OK let me get a few entries from it until i see a problem then i'll post some code to resolve it (if i can) EDIT i assume This Is Seb Clarke - I Just Can't Carry On is formatted correctly Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325259 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Yes it is. All text displayed is formatted correctly just so you know. If you look quickly now - the news is on. This is one I wish to ignore. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325265 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 ok so far i have 30 Seconds to Mars - From Yesterday This Is Seb Clarke - I Just Can't Carry On Overnight_News~1.3 XTREME - HOUR OPENER~1 now i assume you want it to look like this 30 Seconds to Mars - From Yesterday This Is Seb Clarke - I Just Can't Carry On Overnight_News XTREME - HOUR OPENER are the ~1.3 the main thing you want rid of, or are their worse one i havn't seen yet? Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325269 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Very close! Instead of text such as 'Overnight_News~1.3' and 'XTREME - HOUR OPENER~1' being displayed on our website, I want the php to still read this (not choice obviously) but NOT display 'Overnight_News~1.3' and 'XTREME - HOUR OPENER~1' on our website. Instead of those filenames being displayed, I would like 'Swansea University's Favorite Music' to be shown. There are some other ID files and pre-recorded show files too that obviously will appear in the text file but shouldn't be shown on the website. Songs only! Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325276 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 OK erm.. what? i must be having a bad day people are making no sense to me today! Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325278 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Haha! Right. Our audio software outputs whatever is playing to CurrentSong.txt (which is what you can see online). This includes EVERYTHING - songs, station IDs, news, pre-recorded shows whatever mp3 files are playing. This file is uploaded every minute to keep up with any new titles. THEN the php script reads whatever is in that file (artist - songtitle) and does some magic to make it display at www.xtremeradio.org Now, 'bogus' files such as news can be called 'Overnight_News~1.3' or IDs can be 'XTREME - HOUR OPENER~1' or whatever. Now, when Joe Smith logs on to our site and has a look at what is playing, he only wants to see what songs are playing and does not care about IDs, or news etc...it also just looks bad to see 'Now Playing: XTREME - STATION ID 1~1' (The ~1 or ~1.3 or anything similar is totally irrelevent here). So, istead of these 'bogus' files being displayed, in their place, I would like the php to somehow display 'Swansea University's Favorite Music' so it looks nice a neat. So, something like: if CurrentSong.txt has data in it that is 'XTREME - STATION ID 1~1', do NOT output that to xtremeradio.org but output 'Swansea University's Favorite Music' Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325288 Share on other sites More sharing options...
trq Posted August 16, 2007 Share Posted August 16, 2007 As a simple example..... <?php if (strpos($line,'XTREME - STATION ID 1~1')) { echo 'Swansea University's Favorite Music'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325289 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Sounds/looks about right. Will test now... Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325293 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 corrected thopes code <?php if (strpos($line,'XTREME - STATION ID 1~1')) { echo "Swansea University's Favorite Music"; } ?> or anything that contains a ~ <?php if (strpos($line,'~')) { echo "Swansea University's Favorite Music"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325294 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Hmm, getting there but where in my existing php code do I insert this new set of commands? And if so did I include the '<?php' bits? Sorry - not good at php! Just cannot get it to work in the locations I have put it in so far! Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325306 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 $clean3 = str_replace(":", ": ", "$clean2"); //existing line if (strpos($clean3,'XTREME - STATION ID 1~1')) { $clean3 = "Swansea University's Favorite Music"; } Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325324 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Er - '//existing line' is not there at all! Although there is one similar. Either way I cannot get it to work. Ta. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325332 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 Here is the code: <?php // the Current Song Text File $fp = fopen ("http://society.swan.ac.uk/~xtreme/CurrentSong.txt", "r"); // copy the Text file to a variable. $content = fgets( $fp, 60); // Close the file fclose( $fp ); // Cleaning process gets rid of the - and white space and formats in the Song: and line break. $clean1 = str_replace("-", "<br />Song:", "$content"); $clean2 = str_replace(" <br />", "<br />", "$clean1"); $clean3 = str_replace(":", ": ", "$clean2"); // Prints it all out neatly if($content == null) { // If there is no data print an error. echo "<b>Error:</b> Olivers site isn't working!"; } else { //If there are no errors print it all! echo "Artist: "."$clean3"; } echo "<!-- Now Playing Script - copyright Jon Hurlock 2006 -->"; ?> note the line $clean3 = str_replace(":", ": ", "$clean2"); Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-325648 Share on other sites More sharing options...
otronics Posted August 16, 2007 Author Share Posted August 16, 2007 Ah sorry - I gave you the wrong code there but have fixed it. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-326052 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 Just a note: thorpe's code will not work as posted for this example (and some other cases) because strpos returns an integer relating to the position it found the substring. In this case, the substring he suggested is always going to be found at location 0, which php will see as 'false' rendering the if statement useless. to correct this you can use: <?php $substrpos = strpos($line,'XTREME - STATION ID 1~1'); if ($substrpos !== false) { echo 'Swansea University's Favorite Music'; } ?> Even if you are not using this code, it is good to notice things like this. It took me quite a while to figure out what was wrong with my code when I first ran into this issue. Quote Link to comment https://forums.phpfreaks.com/topic/65145-now-playing-development/#findComment-326059 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.