title: Windows-1251 file inside UTF-8 site?
body: <p>Hello everyone Masters Of Web Delevopment :)
  I have a piece of PHP script that fetches last 10 played songs from my winamp. This script is inside file (lets call it "lastplayed.php") which is included in my site with php include function inside a "div".
  My site is on UTF-8 encoding. The problem is that some songs titles are in Windows-1251 encoding. And in my site they displays like "������"...
  Is there any known way to tell to this div with included "lastplayed.php" in it, to be with windows-1251 encoding?
  Or any other suggestions?</p>
  
  <p>P.S: The file with fetching script a.k.a. "lastplayed.php", is converted to UTF-8. But if it is ANCII it's the same result. I try to put  and meta tag with windows-1251 between head tag but nothing happens again.</p>
  
  <p>P.P.S: Script that fetches the Winamp's data (lastplayed.php):</p>
  
  <pre><code>&lt;?php
  /******
  * You may use and/or modify this script as long as you:
  * 1. Keep my name &amp; webpage mentioned
  * 2. Don't use it for commercial purposes
  *
  * If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
  * 
  * Author: Martijn Korse
  * Website: http://devshed.excudo.net
  *
  * Date:  08-05-2006
  ***/
  
  /**
   * version 2.0
   */
  class Radio
  {
      var $fields = array();
      var $fieldsDefaults = array("Server Status", "Stream Status", "Listener Peak", "Average Listen Time", "Stream Title", "Content Type", "Stream Genre", "Stream URL", "Current Song");
      var $very_first_str;
      var $domain, $port, $path;
      var $errno, $errstr;
      var $trackLists = array();
      var $isShoutcast;
      var $nonShoutcastData = array(
                      "Server Status"     =&gt; "n/a",
                      "Stream Status"     =&gt; "n/a",
                      "Listener Peak"     =&gt; "n/a",
                      "Average Listen Time"   =&gt; "n/a",
                      "Stream Title"      =&gt; "n/a",
                      "Content Type"      =&gt; "n/a",
                      "Stream Genre"      =&gt; "n/a",
                      "Stream URL"        =&gt; "n/a",
                      "Stream AIM"        =&gt; "n/a",
                      "Stream IRC"        =&gt; "n/a",
                      "Current Song"      =&gt; "n/a"
                      );
      var $altServer = False;
  
      function Radio($url)
      {
          $parsed_url = parse_url($url);
          $this-&gt;domain   = isset($parsed_url['host']) ? $parsed_url['host'] : "";
          $this-&gt;port = !isset($parsed_url['port']) || empty($parsed_url['port']) ? "80" : $parsed_url['port'];
          $this-&gt;path = empty($parsed_url['path']) ? "/" : $parsed_url['path'];
  
          if (empty($this-&gt;domain))
          {
              $this-&gt;domain = $this-&gt;path;
              $this-&gt;path = "";
          }
  
          $this-&gt;setOffset("Current Stream Information");
          $this-&gt;setFields();     // setting default fields
  
          $this-&gt;setTableStart("&lt;table border=0 cellpadding=2 cellspacing=2&gt;");
          $this-&gt;setTableEnd("&lt;/table&gt;");
      }
  
      function setFields($array=False)
      {
          if (!$array)
              $this-&gt;fields = $this-&gt;fieldsDefaults;
          else
              $this-&gt;fields = $array;
      }
      function setOffset($string)
      {
          $this-&gt;very_first_str = $string;
      }
      function setTableStart($string)
      {
          $this-&gt;tableStart = $string;
      }
      function setTableEnd($string)
      {
          $this-&gt;tableEnd = $string;
      }
  
      function getHTML($page=False)
      {
          if (!$page)
              $page = $this-&gt;path;
          $contents = "";
          $domain = (substr($this-&gt;domain, 0, 7) == "http://") ? substr($this-&gt;domain, 7) : $this-&gt;domain;
  
  
          if (@$fp = fsockopen($domain, $this-&gt;port, $this-&gt;errno, $this-&gt;errstr, 2))
          {
              fputs($fp, "GET ".$page." HTTP/1.1\r\n".
                  "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n".
                  "Accept: */*\r\n".
                  "Host: ".$domain."\r\n\r\n");
  
              $c = 0;
              while (!feof($fp) &amp;&amp; $c &lt;= 20)
              {
                  $contents .= fgets($fp, 4096);
                  $c++;
              }
  
              fclose ($fp);
  
              preg_match("/(Content-Type:)(.*)/i", $contents, $matches);
              if (count($matches) &gt; 0)
              {
                  $contentType = trim($matches[2]);
                  if ($contentType == "text/html")
                  {
                      $this-&gt;isShoutcast = True;
                      return $contents;
                  }
                  else
                  {
                      $this-&gt;isShoutcast = False;
  
                      $htmlContent = substr($contents, 0, strpos($contents, "\r\n\r\n"));
  
                      $dataStr = str_replace("\r", "\n", str_replace("\r\n", "\n", $contents));
                      $lines = explode("\n", $dataStr);
                      foreach ($lines AS $line)
                      {
                          if ($dp = strpos($line, ":"))
                          {
                              $key = substr($line, 0, $dp);
                              $value = trim(substr($line, ($dp+1)));
                              if (preg_match("/genre/i", $key))
                                  $this-&gt;nonShoutcastData['Stream Genre'] = $value;
                              if (preg_match("/name/i", $key))
                                  $this-&gt;nonShoutcastData['Stream Title'] = $value;
                              if (preg_match("/url/i", $key))
                                  $this-&gt;nonShoutcastData['Stream URL'] = $value;
                              if (preg_match("/content-type/i", $key))
                                  $this-&gt;nonShoutcastData['Content Type'] = $value;
                              if (preg_match("/icy-br/i", $key))
                                  $this-&gt;nonShoutcastData['Stream Status'] = "Stream is up at ".$value."kbps";
                              if (preg_match("/icy-notice2/i", $key))
                              {
                                  $this-&gt;nonShoutcastData['Server Status'] = "This is &lt;span style=\"color: red;\"&gt;not&lt;/span&gt; a Shoutcast server!";
                                  if (preg_match("/ultravox/i", $value))
                                      $this-&gt;nonShoutcastData['Server Status'] .= " But an &lt;a href=\"http://ultravox.aol.com/\" target=\"_blank\"&gt;Ultravox&lt;/a&gt; Server";
                                  $this-&gt;altServer = $value;
                              }
                          }
                      }
                      return nl2br($htmlContent);
                  }
              }
              else
                  return $contents;
          }
          else
          {
              return False;
          }
      }
  
      function getServerInfo($display_array=null, $very_first_str=null)
      {
          if (!isset($display_array))
              $display_array = $this-&gt;fields;
          if (!isset($very_first_str))
              $very_first_str = $this-&gt;very_first_str;
  
          if ($html = $this-&gt;getHTML())
          {
               // parsing the contents
              $data = array();
              foreach ($display_array AS $key =&gt; $item)
              {
                  if ($this-&gt;isShoutcast)
                  {
                      $very_first_pos = stripos($html, $very_first_str);
                      $first_pos  = stripos($html, $item, $very_first_pos);
                      $line_start = strpos($html, "&lt;td&gt;", $first_pos);
                      $line_end   = strpos($html, "&lt;/td&gt;", $line_start) + 4;
                      $difference = $line_end - $line_start;
                      $line       = substr($html, $line_start, $difference);
                      $data[$key] = strip_tags($line);
                  }
                  else
                  {
                      $data[$key] = $this-&gt;nonShoutcastData[$item];
                  }
              }
              return $data;
          }
          else
          {
              return $this-&gt;errstr." (".$this-&gt;errno.")";
          }
      }
  
      function createHistoryArray($page)
      {
          if (!in_array($page, $this-&gt;trackLists))
          {
              $this-&gt;trackLists[] = $page;
              if ($html = $this-&gt;getHTML($page))
              {
                  $fromPos    = stripos($html, $this-&gt;tableStart);
                  $toPos      = stripos($html, $this-&gt;tableEnd, $fromPos);
                  $tableData  = substr($html, $fromPos, ($toPos-$fromPos));
                  $lines      = explode("&lt;/tr&gt;&lt;tr&gt;", $tableData);
                  $tracks = array();
                  $c = 0;
                  foreach ($lines AS $line)
                  {
                      $info = explode ("&lt;/td&gt;&lt;td&gt;", $line);
                      $time = trim(strip_tags($info[0]));
                      if (substr($time, 0, 9) != "Copyright" &amp;&amp; !preg_match("/Tag Loomis, Tom Pepper and Justin Frankel/i", $info[1]))
                      {
                          $this-&gt;tracks[$c]['time'] = $time;
                          $this-&gt;tracks[$c++]['track'] = trim(strip_tags($info[1]));
                      }
                  }
                  if (count($this-&gt;tracks) &gt; 0)
                  {
                      unset($this-&gt;tracks[0]);
                      if (isset($this-&gt;tracks[1]))
                          $this-&gt;tracks[1]['track'] = str_replace("Current Song", "", $this-&gt;tracks[1]['track']);
                  }
              }
              else
              {
                  $this-&gt;tracks[0] = array("time"=&gt;$this-&gt;errno, "track"=&gt;$this-&gt;errstr);
              }
          }
      }
      function getHistoryArray($page="/played.html")
      {
          if (!in_array($page, $this-&gt;trackLists))
              $this-&gt;createHistoryArray($page);
          return $this-&gt;tracks;
      }
      function getHistoryTable($page="/played.html", $trackColText=False, $class=False)
      {
          $title_utf8 = mb_convert_encoding($trackArr ,"utf-8" ,"auto");
  
          if (!in_array($page, $this-&gt;trackLists))
              $this-&gt;createHistoryArray($page);
          if ($trackColText)
              $output .= "
              &lt;div class='lastplayed_top'&gt;&lt;/div&gt;
              &lt;div".($class ? " class=\"".$class."\"" : "")."&gt;";
          foreach ($this-&gt;tracks AS $title_utf8)
              $output .= "&lt;div style='padding:2px 0;'&gt;".$title_utf8['track']."&lt;/div&gt;";
          $output .= "&lt;/div&gt;&lt;div class='lastplayed_bottom'&gt;&lt;/div&gt;
          &lt;div class='lastplayed_title'&gt;".$trackColText."&lt;/div&gt;
          \n";
          return $output;
      }
  }
  
   // this is needed for those with a php version &lt; 5
   // the function is copied from the user comments @ php.net (http://nl3.php.net/stripos)
  if (!function_exists("stripos"))
  {
      function stripos($haystack, $needle, $offset=0)
      {
          return strpos(strtoupper($haystack), strtoupper($needle), $offset);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>And the calling script outside the lastplayed.php:</p>
  
  <pre><code>include "lastplayed.php";
  $radio = new Radio($ip.":".$port);
  echo $radio-&gt;getHistoryTable("/played.html", "&lt;b&gt;Last played:&lt;/b&gt;", "lastplayed_content");
  </code></pre>