php如何读取文本指定的内容?

   更新日期:2024.06.02
php读取文件内容:
-----第一种方法-----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;
}
?>

使用循环,下面的语句把需要的内容提取到变量$x中:
$x='';
foreach (file('a.txt') as $line){
if (preg_match('/^12346---(.*?)$/',$line,$reg)){
$x=$reg[1];
break;
}
}

  • 19782576516 :PHP读取TXT中指定位置
    皇知卸1330 :答:<?php //打开文件流,fopen不会把文件整个加载到内存 f = fopen('a.txt','r');//移动文件指针到50 fseek($f,50);//读取50-100字节处的内容 50=100-50 content = fread($f,50);//关闭数据流 fclose($f);//输出内容 echo $content;...
  • 19782576516 :PHP如何读取文件内容?
    皇知卸1330 :答:要读取文件内容,在Php上可以用函数file_get_contents来实现,该函数接收的第一个参数就是文件的路径。很简单,用这个函数就能获取到文件的内容了,代码如图 实际代码里,为了安全起见,我们需要先判断读取的文件是否存在,如果不存在,就没必要读取了。 判断文件是否存在可以用函数file_exists来实现,代码...
  • 19782576516 :php 怎么读取.txt文件中某个字符或某一小段字符
    皇知卸1330 :答:其中我想读取出"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+...
  • 19782576516 :php读取文件的数据,文件名为text.txt,求详细代码。本人新手
    皇知卸1330 :答:echo "文件的内容是:".$con;}else{ echo "文件不存在!";} ?> 第三种读取方式(大文件、循环读取)*** <?php fp = fopen($file_path,"a+");buffer = 1024; //设置读取1024个字节 str = "";//一边读,一边判断是否到达文件末尾 while(!feof($fp)){ str.= fread($fp,$buff...
  • 19782576516 :php如何获取文件内容?
    皇知卸1330 :答:函数把整个文件读入一个字符串中。和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。例如:<?php echo file_get_contents("test.txt");?> ...
  • 19782576516 :php读取文本内容到变量
    皇知卸1330 :答:第一种方法:利用file_get_contents函数直接把文本文件读取成一个字符串 str = file_get_contents("文本文件.txt");echo $str;第二种方法:利用fgets函数 fp = fopen("文本文件.txt","r");str = "";while(! feof($file)){ str .= fgets($file). "";} echo $str;...
  • 19782576516 :php读取文本指定的值
    皇知卸1330 :答:data = json_decode($json, true);content = $data['createForm']['content'];// 注意,这里的$content中包含"\r\n",如果你是保存到普通文件,则他本身就是换行符,所以直接将$content保存到文件即可,如果你是在页面中输出,则有两种方式 // 方式1 contents = explode('\r\n');foreach ...
  • 19782576516 :用PHP实现 读取和修改文本文件内容的代码
    皇知卸1330 :答:读文件 / function read_file($filename){ fp = fopen($filename, "r") or die("couldn't open $filename");read = fread($fp, filesize($filename));fclose($fp);return $read;} / 写文件 / function write_file($filename, $buffer){ fp = fopen($filename, "w") or die("...
  • 19782576516 :php读取txt文件指定行的内容并显示
    皇知卸1330 :答:<?php function getFileRows($filename,$start,$num=0){ rowsdata = array();lines = file( $filename );start = $start -1;num = $num == 0 ? count($lines)-$start : $num;for($i=0;$i<$num; $i++){ k = $start + $i;rowsdata[] = $lines[$k];} return $rowsdata...
  • 19782576516 :怎样用PHP读取一个word文档内容并在浏览器中显示出来
    皇知卸1330 :答://获取htm文件内容并输出到页面 (文本的样式不会丢失)content = file_get_contents($url.$htmlname);echo $content;//获取word文档内容并输出到页面(文本的原样式已丢失)content= $word->ActiveDocument->content->Text;echo $content;//关闭与COM组件之间的连接 word->Documents->close(true);word...
  • 相关链接

    欢迎反馈与建议,请联系电邮
    2024 © 视觉网