Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/65145-now-playing-development/
Share on other sites

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.

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

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.

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?

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!

 

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?  ;)

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";
  }

?>

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

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.

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.