Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 29th, 2006, 01:05 PM
Ralph Höglund
Guest
 
Posts: n/a
Default 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

  #2  
Old January 29th, 2006, 01:35 PM
Jonathan
Guest
 
Posts: n/a
Default 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

  #3  
Old January 29th, 2006, 09:25 PM
Pedro Graca
Guest
 
Posts: n/a
Default 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>
  #4  
Old January 29th, 2006, 10:05 PM
Ralph Höglund
Guest
 
Posts: n/a
Default 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
  #5  
Old January 31st, 2006, 01:45 PM
Alvaro G. Vicario
Guest
 
Posts: n/a
Default 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
--
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.