如何用php读取txt文件里面的单行数据

如何用php读取txt文件里面的单行数据,第1张

首先声明,我下面的代码是以你的那十行数据为基础,测试通过的。但是我得把它的局限说一下。
编码的时候,我是根据你的每一行的规律来的。每行用6个数据分隔7个部分的内容,所以,每个部分不能再有逗号了(当然这个危险主要来自标题,不过我看你标题分隔的时候都是空格或!)
你直接运行吧!
<PHP
$file_name="datatxt"; //假设你的数据是存在这个文件中的
$fp=fopen($file_name,'r');
while(!feof($fp)) //文件全部要读完
{
$buffer=fgets($fp,1024); //获得一行
$period = explode(",",$buffer); //以逗号分隔分行内容
if($period[6]!=1){ //找到第7部分的内容,如果不是1,就输出当然这个也是开关,你可以设置为1时输出
echo $buffer"<br>";
}
}
fclose($fp); //关闭文件流
>

最简单的
file_get_contents('C:\Users\Administrator\Desktop\asdftxt');
<php
$file = fopen('C:\Users\Administrator\Desktop\asdftxt', r);
fread($file, 1024);

<php
//phpinfo();
$a_content = file_get_contents('atxt');
$str = strtok($a_content,"\r");
echo $str;
echo strlen($str);
for($i = 0; $i < strlen($str); $i++)
{
if(is_numeric($str[$i]))
{
echo $str[$i]"<br/>";
}
}
>

<php
function read($file, $mode = 'r')
{
if (is_readable($file)) {
$handle = fopen($file, $mode);
flock($handle, LOCK_EX);
$content = fread($handle, filesize($file));
flock($handle, LOCK_UN);
fclose($handle);
return $content;
}
}
>

$content = file_get_contents('testtxt');
$arr = explode("\n", $content);
echo "<table>";
foreach ($arr as $v) {
$tmp = explode(" ", $v);
echo "<tr>";
echo "<td>" $tmp[0] "</td>";
echo "<td>" $tmp[1] "</td>";
echo "<td>" $tmp[2] "</td>";
echo "<td>" $tmp[3] "</td>";
echo "</tr>";
unset($tmp);
}
echo "</table>";

其中我想读取出"ggdff" 这一小段字符,并把其中每个字符赋上变量$str="aaaaaaabbbbbcccccccccccddddddddd213312ggggdffhhhssd";$str = preg_replace("[\d{6}]", " ", $str);$str=explode(" ",$str);$str2=$str[1];$str2=substr($str2,0,5);$result = trim(preg_replace("/|s+/", " ", $str2));$result =explode(" ",$result);print_r($result);>以上部分为数据处理部分,读取文本内容部分省略掉了 追问: 如果 这个字符串中没有数字也就是 "ggggdff" 之前没 数字"213312"现在只想把文档的里"ggdff" 读取出来我去百度搜索过 , 好象是用" 搜索,截取" 函数功能能实现吧, 你会那种方法吗我自己的想法你看行的通吗


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13379532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-25
下一篇 2023-07-25

发表评论

登录后才能评论

评论列表(0条)

保存