記事の最初の h2 を探しています。見つかったら、次の h2 が見つかるまですべての h3 を探します。すべての見出しと小見出しが見つかるまで、すすいで繰り返します。
この質問を重複した解析の質問としてすぐにフラグを立てるか閉じる前に、質問のタイトルに注意してください。これは基本的なノードの取得に関するものではないためです。私はその部分を下に持っています。
を使用しDOMDocument
て HTML を解析しDOMDocument::loadHTML()
、 記事の重要な見出しを取得するために使用しています。DOMDocument::getElementsByTagName()
DOMDocument::saveHTML()
私のコードは次のとおりです。
$matches = array();
$dom = new DOMDocument;
$dom->loadHTML($content);
foreach($dom->getElementsByTagName('h2') as $node) {
$matches['heading-two'][] = $dom->saveHtml($node);
}
foreach($dom->getElementsByTagName('h3') as $node) {
$matches['heading-three'][] = $dom->saveHtml($node);
}
if($matches){
$this->key_points = $matches;
}
次のような出力が得られます。
array(
'heading-two' => array(
'<h2>Here is the first heading two</h2>',
'<h2>Here is the SECOND heading two</h2>'
),
'heading-three' => array(
'<h3>Here is the first h3</h3>',
'<h3>Here is the second h3</h3>',
'<h3>Here is the third h3</h3>',
'<h3>Here is the fourth h3</h3>',
)
);
私はもっと似たものを探しています:
array(
'<h2>Here is the first heading two</h2>' => array(
'<h3>Here is an h3 under the first h2</h3>',
'<h3>Here is another h3 found under first h2, but after the first h3</h3>'
),
'<h2>Here is the SECOND heading two</h2>' => array(
'<h3>Here is an h3 under the SECOND h2</h3>',
'<h3>Here is another h3 found under SECOND h2, but after the first h3</h3>'
)
);
私はコード補完を正確に探しているわけではありませんが (そうすることで他の人の助けになると思われる場合は、先に進んでください)、上記のようなネストされた配列を実現するための正しい方向への多かれ少なかれガイダンスやアドバイスを探しています。