<?php
$handler=fopen("1.txt","r");
while(!feof($handler))
{
$m = fgets($handler,4096); //fgets逐行读取,4096最大长度,默认为1024
if(substr_count($m,"百度")>0) //查找字符串
{
print_r($m); //打印结果
}
}
fclose($handler); //关闭文件
?> -----第一种方法-----fread()-------- <?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
?>
--------第二种方法------------ <?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-----第三种方法------------ <?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-------第四种方法-------------- <?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>
----第五种方法-------------------- <?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

  

-----第一种方法-----fread()--------

1
2
3
4
5
6
7
8
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
?>

--------第二种方法------------

1
2
3
4
5
6
7
8
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

-----第三种方法------------

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

-------第四种方法--------------

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>

----第五种方法--------------------

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

PHP 如何在txt里查找包含某个字符串的那一行?的更多相关文章

  1. 在某个目录下的所有文件中查找包含某个字符串的Windows命令

    findstr可以完成这个工作.   上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...

  2. svn-checkout后,循环遍历查找包含某字符串的文件

    这里涉及几个知识点: 1.安装subversion,不多说了,网上有教程 2.循环遍历所有目录层级,找相 关文件 #!/bin/bash #########svn checkout项目出来 svn_d ...

  3. [转帖]linux下查找文件及查找包含指定内容的文件常用命令。

    linux下查找文件及查找包含指定内容的文件常用命令. https://blog.csdn.net/yangyu19910407/article/details/18266821 最简单的查找 fin ...

  4. linux下查找文件及查找包含指定内容的文件常用命令

    whereis <程序名称> 查找软件的安装路径-b 只查找二进制文件-m 只查找帮助文件-s 只查找源代码-u 排除指定类型文件-f 只显示文件名-B <目录> 在指定目录下 ...

  5. linux下查找某个目录下包含某个字符串的文件

    有时候要找一些字符串,但是又不知道在哪个文件,只记得一些字符串 那么如何在linux下寻找包含某段文字的文件呢? 强大的find命令可以帮你完成不可能的任务. 比如我只记得我的程序里包含唯一的字符串“ ...

  6. 【原创】python倒排索引之查找包含某主题或单词的文件

    什么是倒排索引? 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射.它是文 ...

  7. SQL判断字符串里不包含字母

    Oracle: 方法一:通过To_Number 函数异常来判断,因为这个函数在转换不成功的时候是报错,所以只能用存储过程包装起来. CREATE OR REPLACE FUNCTION Is_Numb ...

  8. Mysql查找如何判断字段是否包含某个字符串

    Mysql查找如何判断字段是否包含某个字符串   有这样一个需求,在Mysql数据库字符串字段(权限)中,用户有多个不同的邮箱,分别被‘,’分开,现在要取出某个邮箱的所有成员列表.   假设有个表: ...

  9. [转]如何在 Git 里撤销(几乎)任何操作

    任何版本控制系统的一个最有的用特性就是“撤销 (undo)”你的错误操作的能力.在 Git 里,“撤销” 蕴含了不少略有差别的功能. 当你进行一次新的提交的时候,Git 会保存你代码库在那个特定时间点 ...

随机推荐

  1. .net密码找回

    using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using Syst ...

  2. 谈谈bootstrap在实践中的应用

    bootstrap官网是http://www.bootcss.com/ bootstrap的CDN的网址是http://www.bootcdn.cn/ 在平时写的时候尽量用CDN,这样对于网站的运行效 ...

  3. 工作流性能优化(敢问activiti有扩展性?)(1)

    工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试:   之前同事给的解决方案: 1.把&quo ...

  4. Selenium关闭windows系统弹窗

    Selenium关闭windows系统弹窗 背景:在使用某业务时,会弹出windows框 提示要打印某个文本,效果如下,而正常脚本执行完了后,关闭了driver,windows的弹框还是不会消失,这时 ...

  5. 推荐一个WebIDE在线编程语言编译器C9.io

    有时借用别人电脑或者不想在电脑上安装各种乱七八糟的IDE,就可以考虑 Web IDE.随着Web技术发展,很多语言的编译工作都可以利用Web 浏览器来完成. 1. 推荐国外的 C9.io 个人可以免费 ...

  6. java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题

    package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...

  7. mac利用套件管理工具homebrew正确地同时安装python2.7和python3

    MAC OSX 正確地同時安裝 PYTHON 2.7 和 PYTHON3     Python3 出來了(其實已經出來很久了,暈)!但是還是有很多 library 還是使用 Python2.7,所以要 ...

  8. redis Connection refused 远程连接错误

    redis 远程连接时报错:  Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionE ...

  9. mysql 报错 Operand should contain 1 column(s)

    报错 Operand should contain 1 column(s) 原因 select 后面加了 () select (x,x,x)

  10. setInterval与setTimeout

    在自己用canvas画一个时钟时,画秒钟用的是利用图片将重复的线条遮住,但是会出现有两个秒钟线条同时存在,才想起setInterval有那么个坑,查了点资料,记录下,若有不对的或者未写到的点,还请大家 ...