Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件\u获取\u内容或提取前x行的替代方案_Php - Fatal编程技术网

Php 文件\u获取\u内容或提取前x行的替代方案

Php 文件\u获取\u内容或提取前x行的替代方案,php,Php,我想提取html页面的前x行 我知道我可以用这样的方法提取字符的数量: file_get_contents('http://xx.xxx.158.239/fin.html' , NULL, NULL, 0, 125); 但是台词呢?要将文本从第1行提取到第4行吗?这可能吗?您可以使用专用的方法调用来读取文件,而不是对所有文件的get\u contents()进行调用: $fp=fopen('my/file/name','r'); 对于($i=0;$i

我想提取html页面的前x行 我知道我可以用这样的方法提取字符的数量:

file_get_contents('http://xx.xxx.158.239/fin.html' , NULL, NULL, 0, 125);

但是台词呢?要将文本从第1行提取到第4行吗?这可能吗?

您可以使用专用的方法调用来读取文件,而不是对所有
文件的get\u contents()进行调用:

$fp=fopen('my/file/name','r');
对于($i=0;$i<4;$i++){
如果(联邦储备基金($fp)){
回声“到达EOF”;
打破
}
回波fgets($fp);
}
fclose($fp);

您可以使用专用的方法调用来读取文件,而不是使用用于所有
文件获取内容()的方法调用来读取文件。

$fp=fopen('my/file/name','r');
对于($i=0;$i<4;$i++){
如果(联邦储备基金($fp)){
回声“到达EOF”;
打破
}
回波fgets($fp);
}
fclose($fp);
您可以使用创建的文件或URL句柄将内容加载到数组中,并仅读取相关行

要在循环中逐行获取内容,可以使用。

您可以在创建的文件或URL句柄上使用,将内容加载到数组中,并仅读取相关行


要在循环中逐行获取内容,您可以使用。

这里有一个您可能会发现有用的小代码片段:

$file = 'http://xx.xxx.158.239/fin.html';  // remote or local file
$lines = 3; // how many lines do you want?
if (file_exists($file)) {
    $contents = @file_get_contents($file); // suppress errors, esp. for remote files
    $head = implode("\n", array_slice(explode("\n", $contents), 0, $lines));
} else {
    $head = 'File does not exist';
}
echo $head;

下面是一个您可能会发现有用的小代码片段:

$file = 'http://xx.xxx.158.239/fin.html';  // remote or local file
$lines = 3; // how many lines do you want?
if (file_exists($file)) {
    $contents = @file_get_contents($file); // suppress errors, esp. for remote files
    $head = implode("\n", array_slice(explode("\n", $contents), 0, $lines));
} else {
    $head = 'File does not exist';
}
echo $head;

在您的用例中,什么构成了新行?文件中的实际换行符、代码中的html换行标记或其他内容?从您将尝试使用正确格式的文件中获取示例剪报?如果是标准换行格式,fopen和逐行读取将起作用。@miknik换行标记的可能副本在您的用例中构成新行?文件中的实际换行符、代码中的html换行标记或其他内容?从您将尝试使用正确格式的文件中获取示例剪报?如果是标准换行格式,fopen和逐行读取将起作用。@miknik换行标记可能重复