LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2006, 06:47 PM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
PHP preg question


I am trying to take data off of the shoutcast page I use on my Linux box, and create a status page off of it, but I am not having luck on my own.

I want to take this line for example:
[HTML]<tr><td width=100 nowrap><font class=default>Current Song: </font></td><td><font class=default><b>15 Ahhh</b></td></tr>[/HTML]

And pull the data between
[HTML]<tr><td width=100 nowrap><font class=default>Current Song: </font></td><td><font class=default><b>[/HTML]
and
[HTML]</b></td></tr>[/HTML]

Out of this code:

[HTML]<HTML><HEAD><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Expires" content="Mon, 01 Jan 1990 12:00:00 GMT"><title>SHOUTcast Administrator</title><style type="text/css"><!--a:link {color: blue; font-family:Arial, Helvetica; font-size:9pt;}a:visited {color: blue; font-family:Arial, Helvetica; font-size:9pt;}a:hover {color: red; font-family:Arial, Helvetica; font-size:9pt; }.default {color: White; font-family:Arial, Helvetica; font-size:9pt; font-weight: normal}.ST {color: White; font-family:Arial, Helvetica; font-size:8pt; font-weight: normal}.logoText {color: red; font-family: Arial Black, Helvetica, sans-serif; font-size: 25pt; font-weight: normal; letter-spacing : -2.5px;}.flagText {color: blue; font-family: webdings; font-size: 36pt; font-weight: normal; }.ltv {color: blue; font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-weight: normal;}.tnl {color: black; font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; text-decoration: none;}--></style></HEAD><BODY topmargin=0 leftmargin=0 marginheight=0 marginwidth=0 bgcolor=#000000 text=#EEEEEE link=#001155 vlink=#001155 alink=#FF0000><font class=default><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td height=50><font class=flagText>U</font><font class=logoText>&nbsp;SHOUTcast D.N.A.S. Status</font></td></tr><tr><td height=14 align=right><font class=ltv><a id=ltv href="http://www.shoutcast.com/">SHOUTcast Server Version 1.9.5/Linux</a></font></td></tr><tr><td bgcolor=#DDDDDD height=20 align=center><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td align=center><font class=tnl><a id=tnl href="index.html">Status</a></font></td><td align=center><font class=tnl>&nbsp;|&nbsp;</font></td><td align=center><font class=tnl><a id=tnl href="played.html">Song History</a></font></td><td align=center><font class=tnl>&nbsp;|&nbsp;</font></td><td align=center><font class=tnl><a id=tnl href="listen.pls">Listen</a></font></td><td align=center><font class=tnl>&nbsp;|&nbsp;</font></td><td align=center><font class=tnl><a id=tnl href="home.html">Stream URL</a></font></td><td align=center><font class=tnl>&nbsp;|&nbsp;</font></td><td align=center><font class=tnl><a id=tnl href="admin.cgi">Admin Login</a></font></td></tr></table></td></tr></table><br><table cellpadding=5 cellspacing=0 border=0 width=100%><tr><td bgcolor=#000025 colspan=2 align=center><font class=ST>Current Stream Information</font></td></tr></table><table cellpadding=2 cellspacing=0 border=0 align=center><tr><td width=100 nowrap><font class=default>Server Status: </font></td><td><font class=default><b>Server is currently up and private.</b></td></tr><tr><td width=100 nowrap><font class=default>Stream Status: </font></td><td><font class=default><b>Stream is up at 96 kbps with <B>1 of 3 listeners (1 unique)</b></b></td></tr><tr><td width=100 nowrap><font class=default>Listener Peak: </font></td><td><font class=default><b>2</b></td></tr><tr><td width=100 nowrap><font class=default>Average Listen Time: </font></td><td><font class=default><b>1h&nbsp;10m&nbsp;09s</b></td></tr><tr><td width=100 nowrap><font class=default>Stream Title: </font></td><td><font class=default><b>Windisch Radio: Windisch Radio</b></td></tr><tr><td width=100 nowrap><font class=default>Content Type: </font></td><td><font class=default><b>audio/mpeg</b></td></tr><tr><td width=100 nowrap><font class=default>Stream Genre: </font></td><td><font class=default><b>Misc</b></td></tr><tr><td width=100 nowrap><font class=default>Stream URL: </font></td><td><font class=default><b><a href="http://windisch.no-ip.org:8088/wmp.html">http://windisch.no-ip.org:8088/wmp.html</a></b></td></tr><tr><td width=100 nowrap><font class=default>Stream ICQ: </font></td><td><font class=default><b><a href="http://wwp.icq.com/scripts/contact.dll?msgto=1971259">1971259</a></b></td></tr><tr><td width=100 nowrap><font class=default>Stream AIM: </font></td><td><font class=default><b><a href="aim:goim?screenname=draco103">draco103</a></b></td></tr><tr><td width=100 nowrap><font class=default>Stream IRC: </font></td><td><font class=default><b><a href="http://www.shoutcast.com/chat.phtml?dc="></a></b></td></tr><tr><td width=100 nowrap><font class=default>Current Song: </font></td><td><font class=default><b>15 Ahhh</b></td></tr></table><br><table cellpadding=0 cellspacing=0 border=0 width=100%> <tr><td bgcolor=#DDDDDD nowrap colspan=5 align=center><table cellspacing=0 cellpadding=0 border=0><tr><td><font class=ltv>Written by Stephen 'Tag Loomis, Tom Pepper and Justin Frankel</font></td></tr></table></td></tr><tr><td nowrap colspan=5 align=center><font class=ST><b><a href="http://www.shoutcast.com/disclaimer.phtml">Copyright Nullsoft Inc</a><a href="/llamacookie">.</a> 1998-2004</b></font></td></tr></table></font></body></html>[/HTML]
Sorry about this code, thats what I get when I do a view source on the page....


I'm hoping if I could get some help on how to get a simple page that can pull this data, I can then manage to figure the rest out on my own. Any help would be appreciated.
 
Old 05-20-2006, 10:47 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You probably want to have a look at the strip_tags() function. That will make you data much simpler to work with.
 
Old 05-21-2006, 05:13 PM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks that has gotten me started.

This is what I am up to now:
PHP Code:
<?php
function getcontent($server$port$file)
{
   
$cont "";
   
$ip gethostbyname($server);
   
$fp fsockopen($ip$port);
   if (!
$fp)
   {
       return 
"Unknown";
   }
   else
   {
       
$com "GET $file HTTP/1.1\r\nAccept: */*\r\nAccept-Language: de-ch\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\nHost: $server:$port\r\nConnection: Keep-Alive\r\n\r\n";
       
fputs($fp$com);
       while (!
feof($fp))
       {
           
$cont .= fread($fp500);
       }
       
fclose($fp);
       
$cont substr($contstrpos($cont"\r\n\r\n") + 4);
       
$cont strip_tags($cont);

        @
preg_match('/Current Song[:][ ][a-zA-Z0-9]*/'$cont$match);
        
$cont = ((strlen($match[1]) > 1) ? $match[1] : 'N/A');

       return 
$cont;
   }
}
echo 
getcontent("windisch.no-ip.org""8083""/");
?>
I'm having difficulty getting the preg match to work correctly. I'm trying to get everything after "Current Song: " and before "Written by Stephen 'Tag Loomis,". Could someone give me some help with Regular Expressions in PHP?

Thanks
 
Old 05-21-2006, 05:25 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
From there I'd use strstr() to find the location of the substrings and then substr() to extract the data you want.
 
Old 05-22-2006, 09:34 AM   #5
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
That was exactly what I needed, thanks!
Here is what I am up to now:
PHP Code:
<?php

function getcontent($server$port$file)
{
   
$cont "";
   
$ip gethostbyname($server);
   
$fp fsockopen($ip$port);
   if (!
$fp)
   {
       return 
"Unknown";
   }
   else
   {
       
$com "GET $file HTTP/1.1\r\nAccept: */*\r\nAccept-Language: de-ch\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\nHost: $server:$port\r\nConnection: Keep-Alive\r\n\r\n";
       
fputs($fp$com);
       while (!
feof($fp))
       {
           
$cont .= fread($fp500);
       }
       
fclose($fp);
       
$cont substr($contstrpos($cont"\r\n\r\n") + 4);
       
$cont strip_tags($cont);

function 
remover($string$sep1$sep2)
{
       
$string substr($string0strpos($string,$sep2));
#       $string = substr($string, strpos($string,$sep2), 0);
       
$string substr(strstr($string$sep1), 14);


$username="mythtv";
$password="mythtv";
$database="mythconverg";

mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
echo 
"($string)";
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$string%'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo 
"<h1><b><center>Search Results</center></b></h1>";
echo 
"<h3><b>Found $num Matching Songs To Your Search.</b></h3>";
echo 
"<h3><b>Click On The Song Title To Request The Song To Be Played.</b></h3><br>";
$i=0;
while (
$i $num) {

$artist=mysql_result($result,$i,"artist");
$album=mysql_result($result,$i,"album");
$title=mysql_result($result,$i,"title");
$genre=mysql_result($result,$i,"genre");
$year=mysql_result($result,$i,"year");
$length=mysql_result($result,$i,"length");
$length2floor ($length/60000);
$length3fmod ($length60000);
$length2floor ($length2);
#$length3 = $length3 * 60;
$intid=mysql_result($result,$i,"intid");
$filename=mysql_result($result,$i,"filename");

echo 
"<b>Artist: <P2>$artist</P2> Title: <P2><a target='Log' href='request.php?request_id=$intid'>$title</a></P2><br>Album: <P2>$album</P2></b> Year: <P2>$year</P2><br>Song # <P2>$intid</P2> Genre: <P2>$genre</P2><br> Length: <P2>$length ms or $length2:$length3.</P2><br>Filename: $filename<br><hr>";

$i++;
}






       return 
$string;
}

#$string = "Current Song: 10 Walk On The Ocean - Toad the Wet Sprocket Written By blah blah";
$string $cont;
$str1 "Current Song: ";
$str2 "Written";

echo 
remover($string$str1$str2);

$cont " ";
       return 
$cont;
   }
}
echo 
getcontent("windisch.no-ip.org""8083""/");
?>
My only issue that I am having now is that I can't seem to get rid of a space at the end of the string I pull out of the data. I'm using the data to search though mySQL and pull down data. I've tried messing with the code, but can't get rid of that space on the end. Any idea on how to do that?
 
Old 05-22-2006, 09:55 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The trim() function can be used to strip whitespace from both ends of a string, there is also ltrim() and rtrim() to strip it from the start or end respectively.
 
Old 05-23-2006, 08:23 AM   #7
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks graemef, you've been a huge help. I've got the basic frontpage going for what I wanted to do, a lot faster than I thought I could.

I have 1 side question. I am trying to convert milliseconds to minutes:seconds. And I am so close to getting it correct.

This is what I have:
PHP Code:
$length=mysql_result($result,$i,"length");
$length2floor ($length/60000);
$length2floor ($length2);
$length3= ($length - ($length2 60000));
$length3= ($length3 1000);
#       if (strlen($length3) == 1){
#               $length3 = "0$length3";
#                               }
$length3floor ($length3); 

I've got the minutes and seconds perfect, unless a song comes up that is, for example, 1:03. It shows up as 1:3 with what I have. All I need is to add a 0 to the front of the variable when it happens. You can see my pathetic attempt at doing that. Any clues for me?

Last edited by windisch; 05-23-2006 at 08:32 AM.
 
Old 05-23-2006, 08:37 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Look at the sprintf() function:

You will want something like:

$time = sprintf("$d:%02d",$length2,$length3);
 
Old 05-23-2006, 11:41 AM   #9
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Talking

Thanks graemef! That worked perfectly. I think that takes care of this part of my project. If I have any other questions, I'll open a new thread. Thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP question kemplej Linux - Software 1 04-05-2006 07:20 AM
PHP / VideoLAN / Fedora Core Question - how can I get the PHP "exec" function to work gtrawoger Linux - Software 3 12-21-2005 06:51 AM
PHP Question here wh33t Programming 4 12-16-2005 11:02 AM
PHP Question Hockeyfan Programming 5 09-13-2004 06:33 AM
php question philfighter Programming 3 01-30-2002 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration