
正文
php读取文件数据 php如何读取文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php如何获取txt文本指定行的指定数据?
如果直接使用file_get_contents来读取文件,那么在文件很大的时候会很占内容,比如这个文件有1GB的时候。
这个时候使用传统的文件操作方式就好的多,因为是查找嘛,逐行读取匹配应该也是可以的,下面是我的一个建议,不知道是否满足你的要求,可以看下:
//
需要查找的内容
$search
=
'bcd';
//
打开文件
$res
=
fopen('a.txt',
'r');
while
($line
=
fgets($res,
1024))
{
//
根据规则查找
if
(strpos($line,
$search)
===
0)
{
//
根据既定规则取得需要的数据
echo
substr($line,
4,
-1);
//
这里就是你想得到的
break;
}
}
//
关闭文件
fclose($res);
相关问答
Q1: php读取文件的数据,文件名为text.txt,求详细代码。本人新手
h1读取文件内容/h1
***********第一种读取方式********************br
?
$file_path ="test.txt";
if(file_exists($file_path)){ //先判断文件是否存在
//打开文件
$fp = fopen($file_path,"a+");
//读取文件内容
$con = fread($fp,filesize($file_path));
echo "文件的内容是:br".$con;
//在默认情况下,得到的内容输出到网页后,不会换行,因为网页不认\r\n是换行符,把\r\n体换成br /
$con = str_replace("\r\n","br /",$con);
echo "br文件的内容是:br".$con;
//关闭
fclose($fp);
}else{
echo "文件不存在!";
}
?
hr
*************第二种读取方式*******************br
?php
if(file_exists($file_path)){
$con = file_get_contents($file_path);
$con = str_replace("\r\n","br /",$con);
echo "文件的内容是:br".$con;
}else{
echo "文件不存在!";
}
?
hr
************第三种读取方式(大文件、循环读取)**********br
?php
$fp = fopen($file_path,"a+");
$buffer = 1024; //设置读取1024个字节
$str = "";
//一边读,一边判断是否到达文件末尾
while(!feof($fp)){
$str.= fread($fp,$buffer);
}
$str = str_replace("\r\n","br /",$str);
echo $str;
fclose($fp);
?
Q2: php如何读取CSV大文件并且将其导入数据库示例
思路:
读取csv文件,每读取一行数据,就插入数据库
示例
文件夹结构
/
file.csv //csv大文件,这里只模拟三行数据,不考虑运行效率(PS:csv文件格式很简单,文件一般较小,解析很快,运行效率的瓶颈主要在写入数据库操作)
index.php //php文件
file.csv
singi,20
lily,19
daming,23
index.php
/**
* 读取csv文件,每读取一行数据,就插入数据库
*/
//获取数据库实例
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e-getMessage();
}
//读取file.csv文件
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
//写入数据库
$sth = $db-prepare('insert into test set name=:name,age=:age');
$sth-bindParam(':name',$row[0],PDO::PARAM_STR,255);
$sth-bindParam(':age',$row[1],PDO::PARAM_INT);
$sth-execute();
}
fclose($handle);
}
数据表
CREATE TABLE `test` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT '' COLLATE 'utf8mb4_bin',
`age` INT(10) NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_bin'
ENGINE=InnoDB;
运行结束后,数据库中会插入csv中的三行数据
Q3: 想通过PHP实现读取txt文本每次刷新网页随机获取5行数据并输出?
$content = file("test.txt");
$randContent = array_rand($content,5);
echo implode("br /",$randContent);
第一行使用file把把整个文件读入一个数组中
第二行使用array_rand在数组中随机取出5个元素
第三行将取出php读取文件数据的5个数组中间添加br /标签并打印出来
file
把整个文件读入一个数组中
file ( string $filename , int $flags = 0 , resource $context = ? ) : array
array_rand
从数组中随机取出一个或多个随机键
array_rand ( array $array , int $num = 1 ) : int|string|array
implode
将一个一维数组php读取文件数据的值转化为字符串
implode ( string $glue , array $pieces ) : string
Q4: php如何读取文本指定的内容?
php读取文件内容:
-----第一种方法-----fread()--------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小php读取文件数据,这里把整个文件内容读取出来
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)){//循环读取php读取文件数据,直至读取完整个文件
$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;$icount($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参数php读取文件数据,默认是读取1k。
}
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
关于php读取文件数据和php如何读取文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







