DOMDocumentノードのループ内で特定のクラス名(クラス名= 'foo'を持つdiv)で子ノードを検索しようとしています。それが存在する場合には、1に私のFOO値を設定する必要があります。クラス名による子のXPath/Domdocumentチェック
マイHTMLの$文書は次のようになります。
...
<div class="posts">Div Posts 1</div>
<div class="posts">Div Posts 2<div class="foo"></div></div>
<div class="posts">Div Posts 3</div>
<div class="posts">Div Posts 4<div class="foo"></div></div>
<div class="posts">Div Posts 5</div>
...
のDOMDocument/Xpathの($文書):
$html = array();
$document = new \DOMDocument();
$document->loadHTMLFile($url); // loads html from above
$xpath = new \DOMXPath($document);
$i=0;
foreach ($xpath->query(Parser::cssToXpath('.posts')) as $node) {
$html['posts'][$i]['content'] = $node->nodeValue;
// check if child node with class name 'foo' exists => doesn't work :(
$children = $node->getElementsByTagName('foo');
if($children)
$html['posts'][$i]['foo'] = '1';
else
$html['posts'][$i]['foo'] = '0';
$i++;
}
出力:
[posts] => Array
(
[0] => Array
(
[content] => Div class Posts 1
[foo] => 1
)
[1] => Array
(
[content] => Div class Posts 2
[foo] => 1
)
[2] => Array
(
[content] => Div class Posts 3
[foo] => 1
)
[3] => Array
(
[content] => Div class Posts 4
[foo] => 1
)
[4] => Array
(
[content] => Div class Posts 5
[foo] => 1
)
)
getElementsByTagName()は正しいメソッドではないかもしれませんが、すでに別のメソッドを試してみましたが、t彼は右のものです。 :(
VolkerKありがとう!それは私を通過するには時間がかかりましたが、それは魅力的な作品です! :-) どうもありがとう! – Mike
こんにちはVolkerK、置き換えられた要素がどの位置にあるのかわかりますか?例えばどのn番目。DOMツリーの要素にchild()またはeq()が含まれていますか? like(pseudo)$ post-> appendChild ..(... 'クイックキツネ...'); $ post-> GetNthChildorEqNr#()... //私に2を与え、4 – Mike
"// 2と4を与えるだろう" - 申し訳ありませんが、あなたは私をそこに失ってしまいました... – VolkerK