Home  >  Article  >  Backend Development  >  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.

怪我咯
怪我咯Original
2017-07-11 10:28:322367browse

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);
     
?>

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!

Statement:
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