The fgets () function reads a row from the file pointer.
Grammar
Fgets (File,length)
Parameters |
Description |
File |
Necessary. Specifies the file to be read. |
Length |
Optional. Specifies the number of bytes to read. The default is 1024 bytes. |
Description
Reads a row from the file pointed to by the files and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (to see the case first). If length is not specified, the default is 1 K, or 1024 bytes.
If it fails, it returns false.
Hints and Notes
Note: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024 bytes long. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are greater than 8 KB, specifying the length of the largest row in the script is more efficient with resources.
Note: starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not.
Note: You can activate the Auto_detect_line_endings runtime configuration option if you encounter a line terminator for a Macintosh file that is not recognized by PHP when reading a file.
Use the PHP fgets () function below to read an instance of the text file by line, as follows
$handle = @fopen ("D:/public/test.txt", "R"), if ($handle) {while (!feof ($handle)) {$str = Fgets ($handle, 40 96); $str = ' #主单词1 # '; if (Preg_match ('/# (. +) #/', $str, $matches)) {$di _word = $matches [1]; $di _word = mysql_escape_string ($di _word); $sql = "Select di_id from ' du_index ' WHERE Di_word = ' {$di _word} '"; $result = mysql_query ($sql); $row = Mysql_fetch_row ($result); $di _id = $row [0]; if (count ($dy _word) >0) {$sql = "INSERT into ' Du_yun ' (' di_id ', ' Di_word ', ' Dy_word ', ' dy_description ', ' dy_status ', ' d Y_time ') VALUES "; for ($i =0; $i <count ($dy _word), $i + +) {$sql. = "(' {$di _id_1} ', ' {$di _word_1} ', ' {$dy _word[$i]} ', ' {$dy _description[$i ]} ', ' 1 ', now ()), "; } $result = mysql_query (substr ($sql, 0,-1). '; '); if ($result) {}else{echo $sql. ' <br/> '; }} $di _id_1 = $di _id; $di _word_1 = $di _word; $dy _word = $dy _description = Array (); $sql = "; Continue }; $str = ' [volunteers] participate [and] members '; if (Preg_match ('/^\[(. +) \] (. +) $/', $str, $matches)) {$dy _word[] = Trim ($matches [1]); $dy _description[] = Trim ($matches [2]); Continue }} if (count (Dy_word) >0) {$sql = "INSERT into ' Du_yun ' (' di_id ', ' Di_word ', ' Dy_word ', ' dy_description ', ' dy_status ', ' dy_time ') VALUES "; for ($i =0; $i <count ($dy _word), $i + +) {$sql. = "(' {$di _id_1} ', ' {$di _word_1} ', ' {$dy _word[$i]} ', ' {$dy _description[$i ]} ', ' 1 ', now ()), "; } $result = mysql_query (substr ($sql, 0,-1). '; ') or Die (Mysql_error ()); if ($result) {}else{echo $sql. ' <br/> '; }} fclose ($handle);}