2017-10-08 62 views
1

这是我的原字符串: -用什么分裂我的字符串'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)

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

+0

不,我必须保持中断标记。 –

+0

@ chris85伟大的工作....但我需要像'Q.1234)一样的通用',因为你看到之后有整数。 (“/ d”)..现在会是最终的正则表达式 –

回答

1

我认为preg_match_all将是一个更好的功能,因为你想匹配每一行。

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

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

你的正则表达式是看起来有点过于宽松,[Q[.]]+是创建一个字符类,允许Q[,或.]然后试图匹配超过1次,因为]是文字字符。您可以使用自己的[.]来匹配单个期间,或\.是相同的。

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

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

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

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

此方法使用s修改使.延伸。

+0

谢谢。伟大的工作 –

+0

不,使用你的正则表达式我得到我想要的输出。谢谢 –

+0

哦,太好了。我发布了一个更新以及另一个场景的会计。 – chris85