Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

newline - Writing a new line to file in PHP (line feed)

My code:

$i = 0;
$file = fopen('ids.txt', 'w');
foreach ($gemList as $gem)
{
    fwrite($file, $gem->getAttribute('id') . '
');
    $gemIDs[$i] = $gem->getAttribute('id');
    $i++;
}
fclose($file);

For some reason, it's writing as a string, so the file looks like this:

40119
40122
40120
42155
36925
45881
42145
45880

From Google'ing it tells me to use , but is a carriage return which doesn't seem to be what I want to do. I just want the file to look like this:

40119
40122
40120
42155
36925
45881
42145
45880

Thanks.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace ' ' with " ". The escape sequence is not recognized when you use '.

See the manual.

For the question of how to write line endings, see the note here. Basically, different operating systems have different conventions for line endings. Windows uses " ", unix based operating systems use " ". You should stick to one convention (I'd chose " ") and open your file in binary mode (fopen should get "wb", not "w").


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...