我的文件包含此内容(1.txt):
(XXXXXX =数字)
可以只使用user.php?id = XXXXXX并将它们复制到另一个文件,而不会处理不必要的文本,因为文件包含大约50 000行?
答案 0 :(得分:6)
<?php
$file = fopen('source.txt', 'rb');
$newfile = fopen('target.txt', 'wb');
while(($line = fgets($file)) !== false) {
if(strpos($line, 'user.php') !== false) {
fputs($newfile, $line);
}
}
fclose($newfile);
fclose($file);
?>
此代码尚未经过测试,但我认为它可以正常使用。
答案 1 :(得分:3)
<?php
//Get all the matches from the file
$fileContents = file_get_contents('1.txt');
preg_match_all('/user.php\?id=[0-9]{6}/', $fileContents, $matches);
//Output to new file
$fh = fopen('output.txt', 'w+');
foreach ($matches['0'] as $match) {
fputs($fh, $match."\r\n");
}
fclose($fh);
?>
答案 2 :(得分:0)
假设您要将“ abc.php”文件的内容复制到“ working_file.php” 将以下代码放在working_file.php中,它将仅复制所有内容。
<div>
<?php
$file = fopen("abc.php", "r") or die("Unable to open file!");
echo fread($myfile,filesize("abc.php"));
fclose($file);
?>
</div>
答案 3 :(得分:-1)
<?php
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
//echo "File Handling successfully ...";
?>