我有一个字符串,它有这样的内容。
19. Which of the following conflicting criteria does the problem below satisfe. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
a. Writability vs Readability
b. Reliability vs Cost of execution
c. Writability vs Reliability
d. Cost of execution vs. Readability
e. Cost of execution vs. Readability我想要做的是像这样把它分开。
[0] => 19. Which of the following conflicting criteria does the problem below satisfye. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
[1] => a. Writability vs Readability
[2] => b. Reliability vs Cost of execution
[3] => c. Writability vs Reliability
[4] => d. Cost of execution vs. Readability
[5] => e. Cost of execution vs. Readability我的判断力很弱,我有这样的结果。
preg_split('/(?=[a-e\d]+\.(?!\d))/', $entries, -1, PREG_SPLIT_NO_EMPTY);
[0] => 1
[1] => 9. Which of the following conflicting criteria does the problem below satisfy
[2] => e. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
[3] => a. Writability vs Readability
[4] => b. Reliability vs Cost of execution
[5] => c. Writability vs Reliability
[6] => d. Cost of execution vs. Readability
[7] => e. Cost of execution vs. Readability我该怎么做?
发布于 2015-07-06 04:52:33
据我所知,如果有^[a-e\d]+\. 在前面 (启动下一行),您希望在一个或多个垂直空间拆分。拆分函数很好:
$pattern = '/\v+(?=^[a-e\d]+\.)/m';m是用于使^插入符号匹配行开始(不仅仅是字符串开始)的多行标志。
print_r(preg_split($pattern, $str));eval.in测试;应该给出所需的结果:
Array
(
[0] => 19. Which of the following conflicting criteria does the problem below satisfe. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
[1] => a. Writability vs Readability
[2] => b. Reliability vs Cost of execution
[3] => c. Writability vs Reliability
[4] => d. Cost of execution vs. Readability
[5] => e. Cost of execution vs. Readability
)也是用于测试拆分序列的请参阅regex101。如果空行之间有空格,请尝试\s+ (任何空格中的一个或多个),而不是\v+。
发布于 2015-07-06 02:59:02
如果你只想打破每一条线..。
$str = "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
a. Writability vs Readability
b. Reliability vs Cost of execution
c. Writability vs Reliability
d. Cost of execution vs. Readability
";
preg_match_all('/(.*)\n/', $str, $matches);
var_dump($matches);会给你
array(2) {
[0]=>
array(5) {
[0]=>
string(169) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
"
[1]=>
string(32) "a. Writability vs Readability
"
[2]=>
string(38) "b. Reliability vs Cost of execution
"
[3]=>
string(32) "c. Writability vs Reliability
"
[4]=>
string(39) "d. Cost of execution vs. Readability
"
}
[1]=>
array(5) {
[0]=>
string(168) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility."
[1]=>
string(31) "a. Writability vs Readability"
[2]=>
string(37) "b. Reliability vs Cost of execution"
[3]=>
string(31) "c. Writability vs Reliability"
[4]=>
string(38) "d. Cost of execution vs. Readability"
}
}preg_match_all()将使您能够在多行上匹配模式,而$matches数组将为您提供所有匹配。因为它将整行(包括\n)作为匹配处理,所以这是第一个数组。因此,您需要由(.*)匹配的部分,它位于$matches数组的第二个元素中。
https://stackoverflow.com/questions/31237132
复制相似问题