PHP读取文本文件(TXT)
2016-01-04 14:47
573 查看
<? header("content-type:text/html;charset=utf-8"); $file = "demo.txt"; ###判断该文件是否存在 if(file_exists($file)){ $file_arr = file($file); ###得到数组 $arr_new = array(); ####对数组的处理 foreach($file_arr as $v){ $a = trim($v); $a = str_replace("\r\n","",$a); $a = str_replace("\r","",$a); $a = str_replace("\n","",$a); $arr_new[] = $a; } #####接下来就是对数组数据的取出##### }else{ echo "file not exists!"; } ?>
<?php header("content-type:text/html;charset=utf-8"); $file = "demo1.txt"; ###判断该文件是否存在 if(file_exists($file)){ $fp = fopen($file,"a+"); ###已追加的方式打开 $file_size = filesize($file); ###得到文件的大小,单位:字节 ###使用fread函数 $res = fread($fp,$file_size); ####将读取的内容数据的处理 $res_new = str_replace("\r\n",",",$res); ###注意,该替换要用双引号,即"". ###打印结果 ##echo $res_new; var_dump($res_new); ########对字符串数据的取出 }else{ echo "file not exists!"; } fclose($fp); ?>
<?php header("content-type:text/html;charset=utf-8"); $file = "demo2.txt"; if(file_exists($file)){ ####使用 file_get_contents函数 $res = file_get_contents($file); $res_new = str_replace("\r\n",",",$res); ##同上. echo $res_new; ####数据的其他处理 }else{ echo "file not exists!"; } ?>
相关文章推荐
- PHP培训领航者兄弟连IT教育推出兄弟会教育模式
- 读取、设置 php.ini配置文件(复制)
- thinkphp 前台自定义标签中如何应对传递过来的ID?
- php json syntax_error语法错误的一个坑
- <php+mysql>上传文件到指定文件夹,共享数据和脚本,文件验证,脚本之间的通信
- php 获得浏览网页的用户的真实IP
- PHPCMS模版引擎兼容PHP7.0
- 简单的php性能注意点
- win7下php5.5安装solr扩展
- PHP拒绝服务攻击
- PHP 5.3以下版本 无法用下标直接取得函数返回的数组
- PHP在线书签系统分享
- PHP数据库连接mysql与mysqli对比分析
- php中curl的介绍
- 非常重要的php正则表达式详解
- PHPExcel导入导出 若在thinkPHP3.2中使用(无论实例还是静态调用(如new classname或classname::function)都必须加反斜杠,因3.2就命名空间,如/classname
- thinphp的字母方法
- PHP中调用move_uploaded_file函数提示failed to open stream和 Unable to move
- php手册-指令分隔符
- PHP 检查给定的键名或索引是否存在于数组中 array_key_exists 函数