Jump to content

Preg Match


ajicles

Recommended Posts

I need help again with preg match and trying to grab text between two xml tags. And it doesn't return anything back to be I tried adding /s to my pattern for the multi line thing. I am new and getting better at what the patterns mean and stuff.

 

Thanks

~ AJ

 

Code:

 

$server = "68.168.98.191";
$port = "13511";
$password = "********";

$fp = fsockopen("$server", $port, $errno, $errstr, 30);
      fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n");
      while (!feof($fp)) {
          $content = fgets($fp);
}

//echo $content;

      function getTextBetweenTags($string, $tagname)
      {
      $pattern = "/<songtitle>(.*?)<\/songtitle>/s";
      preg_match($pattern, $content, $matches);
      return $matches[1];
      }

 

Echo Content Returns:

<?xml version="1.0" standalone="yes" ?><!DOCTYPE SHOUTCASTSERVER [<!ELEMENT SHOUTCASTSERVER (CURRENTLISTENERS,PEAKLISTENERS,MAXLISTENERS,REPORTEDLISTENERS,AVERAGETIME,SERVERGENRE,SERVERURL,SERVERTITLE,SONGTITLE,SONGURL,IRC,ICQ,AIM,WEBHITS,STREAMHITS,STREAMSTATUS,BITRATE,CONTENT,VERSION,WEBDATA,LISTENERS,SONGHISTORY)><!ELEMENT CURRENTLISTENERS (#PCDATA)><!ELEMENT PEAKLISTENERS (#PCDATA)><!ELEMENT MAXLISTENERS (#PCDATA)><!ELEMENT REPORTEDLISTENERS (#PCDATA)><!ELEMENT AVERAGETIME (#PCDATA)><!ELEMENT SERVERGENRE (#PCDATA)><!ELEMENT SERVERURL (#PCDATA)><!ELEMENT SERVERTITLE (#PCDATA)><!ELEMENT SONGTITLE (#PCDATA)><!ELEMENT SONGURL (#PCDATA)><!ELEMENT IRC (#PCDATA)><!ELEMENT ICQ (#PCDATA)><!ELEMENT AIM (#PCDATA)><!ELEMENT WEBHITS (#PCDATA)><!ELEMENT STREAMHITS (#PCDATA)><!ELEMENT STREAMSTATUS (#PCDATA)><!ELEMENT BITRATE (#PCDATA)><!ELEMENT CONTENT (#PCDATA)><!ELEMENT VERSION (#PCDATA)><!ELEMENT WEBDATA (INDEX,LISTEN,PALM7,LOGIN,LOGINFAIL,PLAYED,COOKIE,ADMIN,UPDINFO,KICKSRC,KICKDST,UNBANDST,BANDST,VIEWBAN,UNRIPDST,RIPDST,VIEWRIP,VIEWXML,VIEWLOG,INVALID)><!ELEMENT INDEX (#PCDATA)><!ELEMENT LISTEN (#PCDATA)><!ELEMENT PALM7 (#PCDATA)><!ELEMENT LOGIN (#PCDATA)><!ELEMENT LOGINFAIL (#PCDATA)><!ELEMENT PLAYED (#PCDATA)><!ELEMENT COOKIE (#PCDATA)><!ELEMENT ADMIN (#PCDATA)><!ELEMENT UPDINFO (#PCDATA)><!ELEMENT KICKSRC (#PCDATA)><!ELEMENT KICKDST (#PCDATA)><!ELEMENT UNBANDST (#PCDATA)><!ELEMENT BANDST (#PCDATA)><!ELEMENT VIEWBAN (#PCDATA)><!ELEMENT UNRIPDST (#PCDATA)><!ELEMENT RIPDST (#PCDATA)><!ELEMENT VIEWRIP (#PCDATA)><!ELEMENT VIEWXML (#PCDATA)><!ELEMENT VIEWLOG (#PCDATA)><!ELEMENT INVALID (#PCDATA)><!ELEMENT LISTENERS (LISTENER*)><!ELEMENT LISTENER (HOSTNAME,USERAGENT,UNDERRUNS,CONNECTTIME, POINTER, UID)><!ELEMENT HOSTNAME (#PCDATA)><!ELEMENT USERAGENT (#PCDATA)><!ELEMENT UNDERRUNS (#PCDATA)><!ELEMENT CONNECTTIME (#PCDATA)><!ELEMENT POINTER (#PCDATA)><!ELEMENT UID (#PCDATA)><!ELEMENT SONGHISTORY (SONG*)><!ELEMENT SONG (PLAYEDAT, TITLE)><!ELEMENT PLAYEDAT (#PCDATA)><!ELEMENT TITLE (#PCDATA)>]><SHOUTCASTSERVER><CURRENTLISTENERS>0</CURRENTLISTENERS><PEAKLISTENERS>1</PEAKLISTENERS><MAXLISTENERS>50000</MAXLISTENERS><REPORTEDLISTENERS>0</REPORTEDLISTENERS><AVERAGETIME>1</AVERAGETIME><SERVERGENRE>Rock</SERVERGENRE><SERVERURL>http://www.listen2myradio.com/</SERVERURL><SERVERTITLE>Ajicles Radio</SERVERTITLE><SONGTITLE>Down With Webster - Rich Girl$</SONGTITLE><SONGURL></SONGURL><IRC>#shoutcast</IRC><ICQ>0</ICQ><AIM>N/A</AIM><WEBHITS>51</WEBHITS><STREAMHITS>2</STREAMHITS><STREAMSTATUS>1</STREAMSTATUS><BITRATE>96</BITRATE><CONTENT>audio/mpeg</CONTENT><VERSION>1.9.8</VERSION><WEBDATA><INDEX>8</INDEX><LISTEN>0</LISTEN><PALM7>0</PALM7><LOGIN>0</LOGIN><LOGINFAIL>0</LOGINFAIL><PLAYED>0</PLAYED><COOKIE>0</COOKIE><ADMIN>0</ADMIN><UPDINFO>8</UPDINFO><KICKSRC>0</KICKSRC><KICKDST>0</KICKDST><UNBANDST>0</UNBANDST><BANDST>0</BANDST><VIEWBAN>0</VIEWBAN><UNRIPDST>0</UNRIPDST><RIPDST>0</RIPDST><VIEWRIP>0</VIEWRIP><VIEWXML>33</VIEWXML><VIEWLOG>0</VIEWLOG><INVALID>2</INVALID></WEBDATA><LISTENERS></LISTENERS><SONGHISTORY><SONG><PLAYEDAT>1279206492</PLAYEDAT><TITLE>Down With Webster - Rich Girl$</TITLE></SONG><SONG><PLAYEDAT>1279206142</PLAYEDAT><TITLE>Swedish House Mafia - One - Original Mix</TITLE></SONG><SONG><PLAYEDAT>1279205829</PLAYEDAT><TITLE>Aaren San - Apes From Space (Dirtyloud Remix)</TITLE></SONG><SONG><PLAYEDAT>1279205509</PLAYEDAT><TITLE>Taio Cruz - Break Your Heart</TITLE></SONG><SONG><PLAYEDAT>1279205509</PLAYEDAT><TITLE>Down With Webster - Rich Girl$</TITLE></SONG><SONG><PLAYEDAT>1279205438</PLAYEDAT><TITLE>Swedish House Mafia - One - Original Mix</TITLE></SONG><SONG><PLAYEDAT>1279205124</PLAYEDAT><TITLE>Aaren San - Apes From Space (Dirtyloud Remix)</TITLE></SONG><SONG><PLAYEDAT>1279204920</PLAYEDAT><TITLE>Taio Cruz - Break Your Heart</TITLE></SONG></SONGHISTORY></SHOUTCASTSERVER>

Link to comment
Share on other sites

Your regular expression is case-sensitive meaning that the lowercase letters will not match the uppercase letters which are present in the XML.  Either change the regex to have uppercase SONGTITLE or use the case-insensitive pattern modifier (/.../si) to match your pattern case-insensitively.

 

Also, why are you using antiquated methods of a) retrieving the XML and b) accessing the SONGTITLE?

Link to comment
Share on other sites

Yay, it works fantasic.  :D Ill I had to is add the i in the pattern and remove the function because it wasn't working, because I tried to echo hello in it and it didn't return anything lol.

 

$server = "68.168.98.191";
$port = "13511";
$password = "";

$fp = fsockopen("$server", $port, $errno, $errstr, 30);
      fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n");
      while (!feof($fp)) {
          $content = fgets($fp);
}
      $pattern = "/<SONGTITLE>(.*?)<\/SONGTITLE>/si";
      preg_match($pattern, $content, $matches);
      echo $matches[1];

Link to comment
Share on other sites

I don't have a Shoutcast server to try this on but the following should, theoretically, also work*. Or at least show you an alternative approach should you want to use it.

 

echo simplexml_load_file("http://68.168.98.191:13511/admin.cgi?pass=&mode=viewxml")->SONGTITLE;

 

 

* Assuming of course that the SimpleXML extension is enabled and it can talk to that URL.

Link to comment
Share on other sites

Well the only thing is it is a remote server and the xml is always changing with the new data. The .htaccess or something isn't allow to access it because it is a server talking to a server not a server talking to a client. So it disallows it to access it.

 

Warning: simplexml_load_file(http://68.168.98.191:13511/admin.cgi?pass=&mode=viewxml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! ICY 404 Resource Not Found in D:\wamp\www\Radioo\xml test.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://68.168.98.191:13511/admin.cgi?pass=&mode=viewxml" in D:\wamp\www\Radioo\xml test.php on line 3

Notice: Trying to get property of non-object in D:\wamp\www\Radioo\xml test.php on line 3

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.