$domxpath->query - 检查标题
$domxpath->query - Check for title
下面的查询仅搜索网站页面上 <h2>
标记后的第一段 "History"
$paragraph = $domxpath->query('
//h2[*[
contains(text(), "History")
]
]
/following-sibling::p[
position() = 1
]'
);
但我想以某种方式检查是否有任何包含历史记录的 <h2>
标签
foreach($paragraph as $node) {
$content= $node->nodeValue;
}
if(!isset($content)){
echo $content;
}else{
echo "static content";
}
这样不行
更新
$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";
}
但我不知道,因为当它没有 "history" 时,它不会打印 "else"
中的静态内容
代码html:
下面div里面有页面的所有主要内容
<div id="mw-content-text" lang="pt" dir="ltr" class="mw-content-ltr">
我想找到 "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>
在开始 <h2>
标签和结束 </h2>
之间有很多代码,可以在上面看到
使用此 XPath 查询获取其中任意位置包含字符串 "History" 的任何 h2
元素:
//h2/*[contains(text(), "History")]
然后,检查结果是否为阳性,统计结果。如果大于0则有结果:
$paragraph = $domxpath->query('//h2/*[contains(text(), "History")]');
if ($paragraph->length > 0) {
echo "Results!";
}
else {
echo "Not contained";
}
下面的查询仅搜索网站页面上 <h2>
标记后的第一段 "History"
$paragraph = $domxpath->query('
//h2[*[
contains(text(), "History")
]
]
/following-sibling::p[
position() = 1
]'
);
但我想以某种方式检查是否有任何包含历史记录的 <h2>
标签
foreach($paragraph as $node) {
$content= $node->nodeValue;
}
if(!isset($content)){
echo $content;
}else{
echo "static content";
}
这样不行
更新
$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";
}
但我不知道,因为当它没有 "history" 时,它不会打印 "else"
中的静态内容代码html:
下面div里面有页面的所有主要内容
<div id="mw-content-text" lang="pt" dir="ltr" class="mw-content-ltr">
我想找到 "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>
在开始 <h2>
标签和结束 </h2>
之间有很多代码,可以在上面看到
使用此 XPath 查询获取其中任意位置包含字符串 "History" 的任何 h2
元素:
//h2/*[contains(text(), "History")]
然后,检查结果是否为阳性,统计结果。如果大于0则有结果:
$paragraph = $domxpath->query('//h2/*[contains(text(), "History")]');
if ($paragraph->length > 0) {
echo "Results!";
}
else {
echo "Not contained";
}