Ask how to split the TXT file by blank line,
such as TXT file a.txt
The contents are as follows:
Oh, ah, yes.
Uh, uh,
111111
222222
Quack Gaga
Quack Gaga
There is a blank line in it, how to press the blank line to cut it into a paragraph of the string?
Reply to discussion (solution)
$content = file_get_contents ('./a.txt ');
$rows = Explode ("\ r \ n", $content);
You can call the function file.
If it is a blank line to split it will find two lines (\ r \ n)
$str = ' 123
Defsf
Abc
1213
&* (
';
Print_r (Explode ("\n\r", $str)); Equivalent to Print_r (Explode ("\r\n\r\n", $str));
or use regular
Preg_match ('/[^.] +? (? =\r\n\r\n)/', $STR, $mat);
Print_r ($mat);
The regular one with Preg_match_all, less writing.
$s =<<< txt ah ah ah uh, uh, uh-huh 111111222222 quack gaga txt; $a = Preg_split ("/([\r\n]+) \\1/", $s);p rint_r ($a);
Array ([0] = AH ah ah uh uh uh, [1] = 111111222222 [2] = = Quack Gaga)
$s =<<< txt ah ah ah uh, uh, uh-huh 111111222222 quack gaga txt; $a = Preg_split ("/([\r\n]+) \\1/", $s);p rint_r ($a);
Array ([0] = AH ah ah uh uh uh, [1] = 111111222222 [2] = = Quack Gaga)
The result of my test of this code is
Array ([0] = AH ah ah uh uh uh, 111111 222222 Quack Gaga)
If the file data is small, consider reading the content one at a time and then splitting it using the Explode method.
If the file is very large, one-time loading will inevitably result in excessive memory consumption and poor performance. This situation can be considered after the open file, read line by row, check the row data to split.
$content = file_get_contents ('./a.txt ');
$rows = Explode ("\ r \ n", $content);
This is separated by a newline instead of separated by a space, and the result is output
Array ([0] = Ah ah ah [1] = uh, uh, [2] = [3] = 111111 [4] = 222222 [5] = [6] = = Gaga [7] [8] ] =)
According to 3 floor friend said use $rows = explode ("\r\n\r\n", $content);
The result of the output is
Array ([0] = AH ah ah uh uh uh, 111111 222222 Quack Gaga)