php中txt如何转成数组(php,编程语言)

时间:2024-05-01 19:50:11 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

php中txt转成数组的方法:1、利用“file_get_contents(txt文件对象)”语句将文件内容读入字符串;2、利用explode()函数将读入的字符串转化为数组即可,语法为“explode("\r\n",字符串对象)”。

本教程操作环境:windows10系统、PHP7.1版、DELL G3电脑

php中txt怎么转成数组

file_get_contents() 把整个文件读入一个字符串中。

该函数是用于把文件的内容读入到一个字符串中的首选方法。如果服务器操作系统支持,还会使用内存映射技术来增强性能。

语法

file_get_contents(path,include_path,context,start,max_length)

explode() 函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组。

语法

explode(separator,string,limit)

示例如下:

文本内容示例:

我爱你中国我爱你NO.1TOP1123456789123456

代码:

/***读取txt文件内容转换成数组*/$file='data.txt';if(file_exists($file)){$content=file_get_contents($file);//文件内容读入字符串if(empty($content)){echo"文件内容为空";}else{$content=mb_convert_encoding($content,'UTF-8','ASCII,UTF-8,GB2312,GBK,BIG5');//转换字符编码为utf-8$array=explode("\r\n",$content);//转换成数组$array=array_filter($array);//去空$array=array_unique($array);//去重print_r(json_encode($array));}}else{echo"文件不存在";}

执行结果:

php中txt如何转成数组

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:php中txt如何转成数组的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python中Airtest自动化测试工具怎么用下一篇:

1 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18