欢迎光临 - 我的站长站,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

php教程

file_get_contents读取TXT指定数据方法

php教程 我的站长站 2025-02-20 共12人阅读

file_get_contents()读取整个文件方法

如果文件不是特别大,你可以简单地使用file_get_contents()读取整个文件内容,然后使用字符串函数(如substr(), strpos(), strstr(), explode()等)来提取或处理特定数据。

$content = file_get_contents('path/to/your/file.txt');
// 例如,提取从第10个字符开始的10个字符
$data = substr($content, 9, 10);
 
// 或者使用 strpos 和 substr 查找特定文本
$start = strpos($content, '特定文本');
if ($start !== false) {
    $data = substr($content, $start, 10);
}

file()或fgets()逐行读取

如果你需要处理大文件或只想读取文件的特定部分,可以使用file()函数逐行读取,或者使用fopen()和fgets()逐行读取。

// 使用 file() 函数读取文件的所有行到一个数组中
$lines = file('path/to/your/file.txt');
foreach ($lines as $line) {
    // 处理每一行
    if (strpos($line, '特定文本') !== false) {
        // 找到包含特定文本的行
        echo $line;
    }
}
 
// 或者使用 fopen() 和 fgets()
$handle = fopen('path/to/your/file.txt', 'r');
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (strpos($line, '特定文本') !== false) {
            // 找到包含特定文本的行
            echo $line;
        }
    }
    fclose($handle);
}

fopen()和fread()部分读取

如果你知道需要读取文件的一部分,可以先使用fopen()打开文件,然后使用fseek()定位到文件中的特定位置,最后使用fread()读取数据。

$handle = fopen('path/to/your/file.txt', 'rb'); // 'rb' 表示以二进制读取模式打开文件
if ($handle) {
    fseek($handle, 10); // 移动到文件的第11个字节(因为偏移量从0开始)
    $data = fread($handle, 10); // 读取接下来的10个字节
    fclose($handle);
}

fopen()和stream_get_contents()读取指定长度的内容

另一种方式是使用stream_get_contents()函数,这在你知道要读取的确切长度时非常有用。

$handle = fopen('path/to/your/file.txt', 'rb'); // 以二进制模式打开文件
if ($handle) {
    fseek($handle, 10); // 移动到文件的第11个字节(因为偏移量从0开始)
    $data = stream_get_contents($handle, 10); // 读取接下来的10个字节
    fclose($handle);
}


相关推荐
  • file_get_contents
  • PHP读取TXT
  • file_get_contents读取TXT指定数据方法

    file_get_contents()读取整个文件方法如果文件不是特别大,你可以简单地使用file_get_contents()读取整个文件内容,然后使用字符串函数(如substr(), strpos(), strstr(), explode()等)来提取或处理特定数据。$content = file_get_contents('path/to/y...

    php教程 12 3周前
  • file_get_contents加载读取TXT文本文件内容方法

    以下是使用file_get_contents()函数加载读取TXT文本文件内容的示例代码:<?php// 文件路径$filePath = &#39;wdzzz/com.txt&#39;; // 检查文件是否存在if (file_exists($filePath)) { // 读取文件内容 $content = file_get_contents($filePath);...

    php教程 108 4个月前
  • file_get_contents函数访问大文件超时解决方法

    file_get_contents函数在访问大文件时,会报错504超时,下面分享下我的站长站的解决方法。$opts = array(&#39;http&#39;=>array(&#39;method&#39;=>"GET",&#39;timeout&#39;=>60,)); $context = stream_context_create($opts); $html =file_get_content...

    php教程 72 2年前
  • file_get_contents函数判断链接是否失效

    在nginx环境中无法使用get_headers函数方法,所以我的站长站这次分享的是用php的file_get_contents函数来判断链接是否失效。原理就是通过file_get_contents函数远程访问链接,判断返回的$http_response_header的HTTP 标头。file_get_contents代码如下:fu...

    php教程 26 2年前
  • PHP读取TXT文本内容方法大全

    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);fcl...

    php教程 14 2周前
  • file_get_contents读取TXT指定数据方法

    file_get_contents()读取整个文件方法如果文件不是特别大,你可以简单地使用file_get_contents()读取整个文件内容,然后使用字符串函数(如substr(), strpos(), strstr(), explode()等)来提取或处理特定数据。$content = file_get_contents(&#39;path/to/y...

    php教程 12 3周前
  • PHP两个TXT文件对比,去除重复内容

    示例代码<?php// 读取两个文件的内容$file1Content = file(&#39;file1.txt&#39;, FILE_IGNORE_NEW_LINES); // 忽略行尾换行符,返回数组$file2Content = file(&#39;file2.txt&#39;, FILE_IGNORE_NEW_LINES); // 忽略行尾换行符,返回数组// 将第二个文...

    php教程 16 3周前
  • file_get_contents加载读取TXT文本文件内容方法

    以下是使用file_get_contents()函数加载读取TXT文本文件内容的示例代码:<?php// 文件路径$filePath = &#39;wdzzz/com.txt&#39;; // 检查文件是否存在if (file_exists($filePath)) { // 读取文件内容 $content = file_get_contents($filePath);...

    php教程 108 4个月前
  • PHP操作TXT写入读取清空方法

    PHP写入TXT<?php$hua = "这是我要写入的一段话";$filename = &#39;name.txt&#39;;$handle =fopen($filename,&#39;a&#39;); fwrite($handle,$hua); fclose($handele);?>PHP读取TXT内容-----第一种方法-----fopen+fread--------<?php$file_path = "te...

    php教程 91 3年前