The query below is only searching for the first paragraph after the <h2>
tag that contains "History" on a page of a website
$paragraph = $domxpath->query('
//h2[*[
contains(text(), "History")
]
]
/following-sibling::p[
position() = 1
]'
);
But I'd like somehow to check whether or not have any <h2>
tag that contains history
foreach($paragraph as $node) {
$content= $node->nodeValue;
}
if(!isset($content)){
echo $content;
}else{
echo "static content";
}
this way it's not working
update
$html = file_get_contents( 'www.site.com' );
$document = new DOMDocument();
$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$domxpath = new DOMXPath($document);
$paragraph = $domxpath->query('
//h2[*[
contains(text(), "History")
]
]
/following-sibling::p[
position() = 1
]'
);
}
foreach($paragraph as $node) {
$content= $node->nodeValue;
}
if(!isset($content)){
echo $content;
}else{
echo "static content";
}
but I do not know because when it does not have "history" it does not print the static content that is inside "else"
code html:
inside the div below has all the main content of the page
<div id="mw-content-text" lang="pt" dir="ltr" class="mw-content-ltr">
I would like to find the that has "History"
<h2><span id="Hist.C3.B3ria"></span><span class="mw-headline" id="History">History</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Adamantina&veaction=edit&section=1" class="mw-editsection-visualeditor" title="Editar secção: History">editar</a><span class="mw-editsection-divider"> | </span><a href="/w/index.php?title=Adamantina&action=edit&section=1" title="Editar secção: History">editar código-fonte</a><span class="mw-editsection-bracket">]</span></span></h2>
between the opening <h2>
tag of closing </h2>
has a lot of code as it is possible to see above
` that contains **History**, if it has history on the page, it only takes the first paragraph if it has no **History** in some `
– Gislef Mar 04 '18 at 20:22` on the page it prints a static content