2017-10-08 91 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