1

YページからHTMLを抽出したい

for example site is
<head>xxxx</head>
<body>....<div id="ineedthis_code"> </div> ...</body>

このfile_get_contentsを実行することは可能ですか?!私はそのdivだけが必要です

4

1 に答える 1

2

特別なライブラリ(ほとんどの場合に最適な方法)を使用せずに、explode関数を使用できます。

$content = file_get_contents($url);
$first_step = explode( '<div id="YOUR ID HERE">' , $content ); // So you will get two array elements

$second_step = explode("</div>" , $first_step[1] ); // "1" depends, if you have more elements with this id (theoretical) 

echo $second_step[0]; // You will get the first element with the content within the DIV :)

これはエラー処理のない例にすぎないことに注意してください。また、特別な場合にも機能します。html構造が変更されていない場合はそうではありません。単純なスペースでさえ、このコードを壊す可能性があります。したがって、解析ライブラリを使用する方がよいでしょう;-)

于 2013-03-26T17:42:20.763 に答える