Home >Backend Development >PHP Tutorial >请问如何按空行来分割txt文件

请问如何按空行来分割txt文件

WBOY
WBOYOriginal
2016-06-13 12:12:321901browse

请教怎么按空行来分割txt文件,
请教怎么按空行来分割txt文件,
比如txt文件 a.txt

内容如下:
啊啊啊啊
呃呃呃

111111
222222

嘎嘎嘎嘎
嘎嘎嘎

里面有空行,怎么按空行把它切割成一段段的字符串呢
------解决思路----------------------
如果是以空行来分割就要找两个换行(\r\n)了
$str='123
defsf

abc
1213

&*(
';
print_r(explode("\n\r",$str)); //相当于print_r(explode("\r\n\r\n",$str)); 
或者用正则
preg_match('/[^.]+?(?=\r\n\r\n)/',$str,$mat);
print_r($mat);
------解决思路----------------------

$s =<<< TXT<br />啊啊啊啊<br />呃呃呃<br /><br />111111<br />222222<br /><br />嘎嘎嘎嘎<br />嘎嘎嘎<br />TXT;<br /><br />$a = preg_split("/([\r\n]+)\\1/", $s);<br />print_r($a);
Array<br />(<br />    [0] => 啊啊啊啊<br />呃呃呃<br />    [1] => 111111<br />222222<br />    [2] => 嘎嘎嘎嘎<br />嘎嘎嘎<br />)<br /><br />

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