Newline doesn't work always....??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Höglund

    #1

    Newline doesn't work always....??

    The \n I write out to an open file does not work
    the second time:

    $xmlstr = "<?xml version=\"1.0\" \x3F>\n";
    $songstr = "<songs>\n" ;

    fwrite($handle, "$xmlstr"); //after this I got a new line
    fwrite($handle, "$songstr") ; //but not after this

    $lines = file("./clips/$file_name");
    foreach ($lines as $line_num => $line) {

    fwrite($handle, " <song path=\"$line"); //$line is from
    another file containing path and song text.
    }

    The output:

    <?xml version="1.0"?>
    <songs> <song path="http://209.245.59.124/song.mp3

    Should be:

    <?xml version="1.0"?>
    <songs>
    <song path="http://209.245.59.124/song.mp3


    What do I do wrong??

    Ralph
  • Jonathan

    #2
    Re: Newline doesn't work always....??

    Ralph Höglund wrote:[color=blue]
    > The \n I write out to an open file does not work
    > the second time:
    >
    > $xmlstr = "<?xml version=\"1.0\" \x3F>\n";
    > $songstr = "<songs>\n" ;
    >
    > fwrite($handle, "$xmlstr"); //after this I got a new line
    > fwrite($handle, "$songstr") ; //but not after this[/color]

    I don't know what is the solution to your problem, but you certainly do
    not have to surround variables with quotes to write them to a file. This
    would do:

    fwrite($handle, $xmlstr);

    If you want to insert a variable in a string then you need quotes:

    fwrite($handle, "The contents of this string is $xmlstr");
    [color=blue]
    > $lines = file("./clips/$file_name");
    > foreach ($lines as $line_num => $line) {
    >
    > fwrite($handle, " <song path=\"$line"); //$line is from another file
    > containing path and song text.[/color]

    And I guess this line should read

    fwrite($handle, " <song path=\"$line\"> ");

    Jonathan

    Comment

    • Pedro Graca

      #3
      Re: Newline doesn't work always....??

      Ralph Höglund wrote:[color=blue]
      > The \n I write out to an open file does not work
      > the second time:
      >
      > $xmlstr = "<?xml version=\"1.0\" \x3F>\n";
      > $songstr = "<songs>\n" ;
      >
      > fwrite($handle, "$xmlstr"); //after this I got a new line
      > fwrite($handle, "$songstr") ; //but not after this[/color]
      <snip>

      in what mode did you fopen the file?

      /* open $filename in text mode */
      $handle = fopen($filename , "wt");

      --
      If you're posting through Google read <http://cfaj.freeshell. org/google>

      Comment

      • Ralph Höglund

        #4
        Re: Newline doesn't work always....??

        Pedro Graca skrev:[color=blue]
        > Ralph Höglund wrote:
        >[color=green]
        >>The \n I write out to an open file does not work
        >>the second time:
        >>
        >>$xmlstr = "<?xml version=\"1.0\" \x3F>\n";
        >>$songstr = "<songs>\n" ;
        >>
        >> fwrite($handle, "$xmlstr"); //after this I got a new line
        >> fwrite($handle, "$songstr") ; //but not after this[/color][/color]

        Thanks all, I found out that I needed to use both \r and \n to
        get it working. I am slowly getting the grasp of php programming now.
        [color=blue]
        >
        > <snip>
        >
        > in what mode did you fopen the file?
        >
        > /* open $filename in text mode */
        > $handle = fopen($filename , "wt");
        >[/color]
        Well, I opened it with "w", no problem with that.

        I had also to trim the lines from my m3u-file to get rid of
        return and newline chars.

        foreach ($lines as $line_num => $line) {
        echo $line;
        $line = trim($line);
        fwrite($handle, " <song path=\"$line\"\ r\n");
        }
        fclose($handle) ;

        Now I have left the extraction of the song title to be invoked
        into the end of lines as title = "song.mp3">

        Will be back when (if) new problems arise.

        Ralph in Sweden

        Comment

        • Alvaro G. Vicario

          #5
          Re: Newline doesn't work always....??

          *** Ralph Höglund escribió/wrote (Sun, 29 Jan 2006 22:52:50 GMT):[color=blue]
          > Thanks all, I found out that I needed to use both \r and \n to
          > get it working. I am slowly getting the grasp of php programming now.[/color]

          Well, that's a different issue. You need to use \r\n is you want to open
          the file with Windows notepad, because that's the line ending convention in
          windows. Linux/Unix use \n and (if I recall correctly, Mac computers use
          \r). But you can always read the file correctly if you use a proper text
          editor.



          --
          -+ Álvaro G. Vicario - Burgos, Spain
          ++ http://bits.demogracia.com es mi sitio para programadores web
          +- http://www.demogracia.com es mi web de humor libre de cloro
          --

          Comment

          Working...