Shoutbox

PHP Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Help (/showthread.php?tid=25513)

PHP Help by DJeX on 05-20-2004 at 01:02 AM

I'm trying to write an .asx file with a PHP code. But can't because the PHP wont let me add " " (quotations) to the script. It only allows me to add ' ' (commas). Heres the code I'm using. It picks a random song and writes it to an .asx file. Then redirects to an embedded media player to play the .asx file.

code:
<?php 
$files = getRandomMedia('../media/',320,240,20);     //call to media directory

function getRandomMedia($dir,$h,$w,$b)
{
$movie = getRandomClip($dir);

$s  = '"'.$dir.$movie.'" ';


return $s;
}
function getRandomClip($dir,$type='random')
{
global $errors;

  if (is_dir($dir)) { 

  $fd = opendir($dir); 
  $images = array();

      while (($part = @readdir($fd)) == true) { 

      clearstatcache();

          if ( eregi("(avi|mpg|mp3|mov|wav)$",$part) ) {
              $images[] = $part;
          }
      }

    // adding this in case you want to return the image array
    if ($type == 'all') { return $images; }

    // Be sure to call srand() once per script
    srand ((double) microtime() * 1000000);
    $key = rand (0,sizeof($images)-1);

    return $images[$key];

  } else {
      $errors[] = $dir.' is not a directory';
      return false;
  }
}

$filename = 'playlist.asx';
$fp = fopen($filename, "w");
$write = fwrite($fp, "
<ASX version = "3.0">

<TITLE>Xplode Radio</TITLE>
<Entry>
<Ref href = "$files" />
<Ref href = "../media/Light of the Dark.mp3" />
<Ref href = "../media/Tranced.mp3" />     
</Entry>

</ASX>
");
fclose($fp);
?>

<script language="javascript">
<!--
location.replace("/radio.htm");
-->
</script>


This is the part I need to be written exactly like this, but because of the " " I can't.

code:
<ASX version = "3.0">

<TITLE>Xplode Radio</TITLE>
<Entry>
<Ref href = "$files" />
<Ref href = "../media/Light of the Dark.mp3" />
<Ref href = "../media/Tranced.mp3" />     
</Entry>

</ASX>


Is there any way to get it to write in the file exactly like this?


I also need to write each line separately, not like this:

<ASX version = '3.0'>[]<TITLE>Xplode Radio</TITLE>[]<Entry>[]<Ref href = '"../media/Light of the Dark.mp3" ' />[]<Ref href = '../media/Light of the Dark.mp3' />[]<Ref href = '../media/Tranced.mp3' />[]</Entry>[][]</ASX>

*[] = the symbol for next line


And if you see any errors in my code PLEASE tell me.

Thank You!
RE: PHP Help by WDZ on 05-20-2004 at 04:41 AM

So the problem is at the fwrite() line, right? If you want to have double quotes (") inside a string that's enclosed by double quotes, you need to escape all the quotes in the string. For example:

code:
fwrite($fp, "<ASX version = \"3.0\">

<TITLE>Xplode Radio</TITLE>
<Entry>
<Ref href = \"$files\" />
<Ref href = \"../media/Light of the Dark.mp3\" />
<Ref href = \"../media/Tranced.mp3\" />     
</Entry>

</ASX>");

(Note the escape character (backslash) before the " characters)

About the new lines: if you see all the lines together with strange symbols where they should break, it's possible that the file is being created in UNIX format, and your text viewer doesn't support it. Try using binary mode when you upload the PHP script to your server... that should keep the Windows-style line breaks. If that doesn't work, you could change that piece of code again to this:
code:
fwrite($fp, "<ASX version = \"3.0\">\r\n\r\n<TITLE>Xplode Radio</TITLE>\r\n<Entry>\r\n<Ref href = \"$files\" />\r\n<Ref href = \"../media/Light of the Dark.mp3\" />\r\n<Ref href = \"../media/Tranced.mp3\" />\r\n</Entry>\r\n\r\n</ASX>");

RE: PHP Help by DJeX on 05-22-2004 at 06:17 AM

Workrd Great! Thanks! :D(Y)