使用`preg_split`分割我的String的内容

时间:2017-10-08 17:04:57

标签: php regex preg-match-all 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 />
";

我希望以这种格式将我的String拆分为数组

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)

但我在正则表达方面表现不佳。 请帮助我将我的正则表达式纳入相应的..

1 个答案:

答案 0 :(得分:1)

我认为<!DOCTYPE html> <html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } </style> </head> <body> <table> <tr> <th>Description</th> <th>Order_Number</th> </tr> <?php foreach ($yourArry as $row) { echo "<tr>"; echo "<td>".$row["Description"]."</td>"; echo "<td>".$row["Order_Number"]."</td>"; echo "</tr>"; } ?> </table> </body> </html> 对你来说会更好,因为你想匹配每一行。

preg_match_all

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

你的正则表达式看起来有点松散,preg_match_all('/Q\.\d+.*/', $data, $matches); print_r($matches[0]); 正在创建一个允许[Q[.]]+Q[的字符类。然后尝试匹配.超过1次,因为]是文字字符。您可以单独使用]来匹配单个句点,或[.]是相同的。

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

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

.*

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

此方法使用/(Q\.\d+.*?)(?:(?:<br \/>|\n){3}|$)/ 修饰符,以便s扩展。