0

内部からのみHTMLコンテンツを取得する方法を見つけるのに問題があります

PHP5のタグ。

次のドキュメントの例を取り上げ、2つ(またはそれ以上のプレタグ領域、その動的)を取り、それを配列に押し込みます。

blablabla
<pre>save
this
really</pre>
not this
<pre>save this too
really
</pre>
but not this

別のサーバー上のhtmlファイルのpreタグ間の領域を配列に押し込むにはどうすればよいですか。

4

3 に答える 3

1

xpathの使用をお勧めします

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DomXpath($doc);

$pre_tags = array();
foreach($xpath->query('//pre') as $node){
    $pre_tags[] = $node->nodeValue;
}
于 2011-11-09T05:08:33.007 に答える
0

HTMLが整形式であると仮定すると、次のようなことができます。

$pos = 0;
$insideTheDiv = array();
while (($pos = strpos($theHtml, "<pre>", $pos)) !== false) {
    $pos += 5;
    $endPrePos = strpos($theHtml, "</pre>", $pos);
    if ($endPrePos !== false) {
        $insideTheDiv[] = substr($theHtml, $pos, $endPrePos - $pos);
    } else break;
}

完了すると、タグ$insideTheDivのすべてのコンテンツの配列になります。pre

デモ:http ://codepad.viper-7.com/X15l7P (出力から改行を削除します)

于 2011-11-09T03:31:52.630 に答える
0

正規表現を使用して、preタグ内のすべてのコンテンツを抽出できます。

Pythonでは次のようになります。

re.compile('<pre>(.*?)</pre>', re.DOTALL).findall(html)
于 2011-11-14T16:32:44.137 に答える