What is the code for php to read the last few lines of data from the file?

青灯夜游
Release: 2023-03-13 09:08:01
Original
2548 people have browsed it

In PHP, you can use the file() and array_slice() functions to read the last few lines of data in the file and implement the code "array_slice(file($file,FILE_IGNORE_NEW_LINES), - number of lines)".

What is the code for php to read the last few lines of data from the file?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php Read the last few lines of data from the file

Implementation idea:

  • Use the file() function to store the entire file data into an array, each line The data corresponds to an array element

  • . Just use the array_slice() function to get the last few elements in the array.

Implementation code:

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$file = &#39;test.txt&#39;;
$filearr = file($file,FILE_IGNORE_NEW_LINES);

echo "文件的数据:";
var_dump($filearr);

echo "截取数组后2位的元素片段:";
$result = array_slice($filearr,-2); //截取数组后2位的元素
var_dump($result);
?>
Copy after login

Output result:

What is the code for php to read the last few lines of data from the file?

Description:

file() The function will store the contents of the file into an array line by line (including newlines). This array is returned on success, FALSE on failure. The syntax format of the file() function is as follows:

file($filename,$flags,$context)
Copy after login

This function accepts one required parameter $filename (the file to be read), and two omitted parameters $flags and $context (the environment of the file handle).

But generally the $flags parameter is still set, which can be one or more of the following constants:

  • ##FILE_USE_INCLUDE_PATH: Search for files in include_path (in php.ini), the default is FALSE; if you want, set the parameter value to '1'.

  • FILE_IGNORE_NEW_LINES: Do not add a newline character at the end of each element of the array;

  • FILE_SKIP_EMPTY_LINES: Skip empty lines.

array_slice() function is a function provided by PHP to intercept an array, and can extract a fragment from the array. The syntax is as follows:

array array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )
Copy after login
Parameter description:

    arr represents the array to be intercepted.
  • start indicates the starting position (subscript) of interception:
    • If start is a positive number, interception will be performed from front to back.
    • If start is a negative number, start from the position -start from the end of arr and intercept from back to front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the intercepted length:
    • If length is a positive number, it indicates the number of intercepted elements;
    • If length If it is a negative number, then the intercepted fragment will end at the length position from the end of the array;
    • If it is omitted, it will start from the start position and continue to the end of the array.
  • preserve_keys is an optional parameter that specifies whether to retain the original key name. The default is false, that is, not retained; if set to true, the original key name will be retained.
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the code for php to read the last few lines of data from the file?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template