使用 `preg_split` 拆分我的字符串的内容

What to split my String using `preg_split`

这是我的原始字符串 :-

$data = "<br />
Q.156)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />

<br />
Q.157)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.158)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.159)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.160)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.161)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.162)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
";

我想将我的字符串拆分为这种格式的数组

Array (
[0] => Q.156) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[1] => Q.157) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[2] => Q.158) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[3] => Q.159) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[4] => Q.160) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[5] => Q.161) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[6] => Q.162) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 
)

我知道我可以使用 preg_split('/[Q[.]]+/', $data)

但我在正则表达式方面有点弱。 请帮助我的正则表达式相应地合并..

我认为 preg_match_all 对你来说是一个更好的函数,因为你想匹配每一行。

preg_match_all('/Q\.\d+.*/', $data, $matches);
print_r($matches[0]);

演示:https://3v4l.org/pWs6c

您的正则表达式看起来有点过于松散,[Q[.]]+ 正在创建字符 class 允许 Q[.。然后尝试匹配 ] 超过 1 次,因为 ] 是文字字符。您可以单独使用 [.] 来匹配单个句点,或者 \. 相同。

\d 是一个数字。
.* 允许零个或多个任意字符,不包括新行,因此这会捕获您的每个问题行。

如果您还需要结尾行,您可以使用这个修改后的正则表达式。

/(Q\.\d+.*?)(?:(?:<br \/>|\n){3}|$)/

演示:https://regex101.com/r/Joo8wt/1/

此方法使用 s 修饰符,因此 . 会扩展。