duangou6446
duangou6446
2013-11-21 13:16

读取文件保留换行符和空格

已采纳

I'm writing a PHP script which stores the values in the file in an array. I want newlines and spaces to be preserved.

The text file looks like below:

abc

pqr

xyz

What I thought is to use cat Linux command to read the file and then direct it's value in an array.

This is what I tried:

$input = '/var/www/oj/1.in';
$array = system(cat $input);

That didn't work. Any ideas?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtjo87679 dtjo87679 8年前

    Try the function that is used for this:

    $array = file($input);
    print_r($array);
    

    But since you don't actually want an array as stated in the question, try:

    $output = file_get_contents($input);
    

    Or to skip the empty lines:

    $output = implode(file($input, FILE_SKIP_EMPTY_LINES));
    
    点赞 评论 复制链接分享