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.
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!