下面是一个简单的示例代码,实现了打开文件、逐行读取、去除重复路径、保存文件等功能:

<?php
// 打开文件
$file = fopen("t.txt", "r");

// 逐行读取
$result = array();
while (!feof($file)) {
    $line = fgets($file);
    $line = trim($line);

    // 去除重复路径
    $parts = parse_url($line);
    if (isset($parts['path'])) {
        $path = $parts['path'];
        $path = preg_replace('/\/+/', '/', $path);
        $line = str_replace($parts['path'], $path, $line);
    }

    $result[] = $line;
}

// 关闭文件
fclose($file);

// 保存文件
$file = fopen("t.txt", "w");
fwrite($file, implode("\n", $result));
fclose($file);
?>

上述代码中,我们首先打开文件,然后使用fgets()函数逐行读取文件内容,去除重复路径后将每一行的内容保存到一个数组中。

接着,我们使用implode()函数将数组中的内容连成一个字符串,并且使用fwrite()函数将这个字符串写入到文件中。

最后,我们使用fclose()函数关闭文件句柄,完成文件的读取和写入操作

用php写一个打开ttxt文件把每一行的链接重复路径删除掉然后保存

原文地址: https://www.cveoy.top/t/topic/eA9f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录