内部からのみHTMLコンテンツを取得する方法を見つけるのに問題があります
とPHP5のタグ。
次のドキュメントの例を取り上げ、2つ(またはそれ以上のプレタグ領域、その動的)を取り、それを配列に押し込みます。
blablabla
<pre>save
this
really</pre>
not this
<pre>save this too
really
</pre>
but not this
別のサーバー上のhtmlファイルのpreタグ間の領域を配列に押し込むにはどうすればよいですか。
内部からのみHTMLコンテンツを取得する方法を見つけるのに問題があります
とPHP5のタグ。
次のドキュメントの例を取り上げ、2つ(またはそれ以上のプレタグ領域、その動的)を取り、それを配列に押し込みます。
blablabla
<pre>save
this
really</pre>
not this
<pre>save this too
really
</pre>
but not this
別のサーバー上のhtmlファイルのpreタグ間の領域を配列に押し込むにはどうすればよいですか。
xpathの使用をお勧めします
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DomXpath($doc);
$pre_tags = array();
foreach($xpath->query('//pre') as $node){
$pre_tags[] = $node->nodeValue;
}
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 (出力から改行を削除します)
正規表現を使用して、preタグ内のすべてのコンテンツを抽出できます。
Pythonでは次のようになります。
re.compile('<pre>(.*?)</pre>', re.DOTALL).findall(html)