我有一个这样的字符串:
== Paragraph ==
=== title 1 ===
content
=== Title 2 ===
other content
== Paragraph 2 ==
=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf
=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg
我试图将它解析成这样的数组:
$arr = array(
array("title"=>"Paragraph", "content" => "=== title 1 ===
content
=== Title 2 ===
other content "),
array("title"=>"Paragraph 2", "content" => "=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf
=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg"));
我已经尝试过这个表达式来获取段落名称:
preg_match_all("@== (.*?) ==.*?@is", $data, $paragraphs);
但是这个表达式没有按我的意愿工作,因为它与整个段落不匹配输出是这样的并且它没有得到内容:
Array
(
[0] => Paragraph
[1] => title 1
[2] => Title 2
[3] => Paragraph 2
[4] => asd1
[5] => asd2
)
我也试过(nl2br 因为我不知道如何在正则表达式中使用新行)
$data = nl2br($data);
preg_match_all("@== (.*?) ==<br />.*?@is", $data, $paragraphs);
但结果是这样的:
[0] => Array
(
[0] => == Paragraph == <br />
=== title 1 ===<br />
content<br />
<br />
=== Title 2 ===<br />
other content <br />
<br />
== Paragraph 2 ==<br />
)
我不理解正则表达式,我不知道如何解决我的问题。