Ask how to split the TXT file by blank line,

Source: Internet
Author: User
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)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.