Home > Backend Development > PHP Tutorial > PHP uses the fgets() function to read the string line by line and then uses the explode() function to split the string into array instances.

PHP uses the fgets() function to read the string line by line and then uses the explode() function to split the string into array instances.

怪我咯
Release: 2023-03-13 13:46:01
Original
2425 people have browsed it

fgets() Function Read a line from the file pointer. The explode() function breaks up the string into an array.

The following example code first uses the fgets() function to read the string line by line and then uses the explode() function to split the string into an array. The code is as follows

<!--?php
    $f= fopen("file.txt","r");
    while (!feof($f))
    {
        $line = fgets($f);
        echo $line,"<br /-->";
     
        $str = explode("|",$line);
        //print_r($str);
     
        $file_type =  $str [0];
        $dir_name =  $str [1];
        $file_name =  $str [2];
        $file_size =  $str [3];
        $create_time =  $str [4];
 
        echo $file_type;
        echo "<br>";
     
        echo $dir_name;
        echo "<br>";
     
        echo $file_name;
        echo "<br>";
 
        echo $file_size;
        echo "<br>";
     
        echo $create_time;
        echo "<br>";
 
    }
    fclose($f);
     
?>
Copy after login

The above is the detailed content of PHP uses the fgets() function to read the string line by line and then uses the explode() function to split the string into array instances.. 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