使用php从txt文件中删除换行符 [英] remove new line characters from txt file using php

查看:152
本文介绍了使用php从txt文件中删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有txt文件,它的内容是这样

I have txt file its content like this

Hello  
World   
John  
play  
football  

阅读此文本文件时我想删除换行符,但我不知道它的外观 文件.txt及其编码为utf-8

I want to delete the new line character when reading this text file, but I don't know how it look like the file .txt and its encoding is utf-8

推荐答案

只需将file函数与FILE_IGNORE_NEW_LINES标志一起使用.

Just use file function with FILE_IGNORE_NEW_LINES flag.

file读取整个文件,并返回包含所有文件行的数组.

The file reads a whole file and returns an array contains all of the file lines.

默认情况下,每行末尾都包含换行符,但是我们可以通过FILE_IGNORE_NEW_LINES标志强制进行修剪.

Each line contains new line character at their end as default, but we can enforce trimming by FILE_IGNORE_NEW_LINES flag.

所以这很简单:

$lines = file('file.txt', FILE_IGNORE_NEW_LINES);

结果应为:

var_dump($lines);
array(5) {
    [0] => string(5) "Hello"
    [1] => string(5) "World"
    [2] => string(4) "John"
    [3] => string(4) "play"
    [4] => string(8) "football"
}

这篇关于使用php从txt文件中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆