我在努力從字符串中提取內容(存儲在數據庫中)。 每個div是一個章節,而h2內容是標題。我想separatly提取我已經在PHP preg_match_all tryed標題和每章(格)的內容奮力從字符串中提取內容(PHP)
<p>
<div>
<h2>Title 1</h2>
Chapter Content 1 with standard html tags (ex: the following tags)
<strong>aaaaaaaa</strong><br />
<em>aaaaaaaaa</em><br />
<u>aaaaaaaa</u><br />
<span style="color:#00ffff"></span><br />
</div>
<div>
<h2>Title 2</h2>
Chapter Content 2
</div>
...
</p>
,但是當我標準的HTML標籤
function splitDescription($pDescr)
{
$regex = "#<div.*?><h2.*?>(.*?)</h2>(.*?)</div>#";
preg_match_all($regex, $pDescr, $result);
return $result;
}
這是行不通的
使用正則表達式解析HTML只是本身就是一個壞主意,使用DOM文檔的一個實例來分析你的HTML。 – 2012-07-19 17:17:10
你是否有一羣html解析器 - [DOMDocument](http://php.net/manual/en/class.domdocument.php),[SimpleXml](http://php.net/manual/en/book.simplexml .php)也看到這個http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Musa 2012-07-19 17:20:45
謝謝你的建議:) – 2012-07-19 17:34:03