我有这个SEO脚本,我正在我的$row['content']中查找$row_meta['keyword']这是我的搜索焦点关键字。
我想检查h1、h2、h3或h4是否标记了我的关键字。例如,如果my$row_meta['keyword']="test"此文本应该匹配:
This is my text
<h2>We just want to test</h2>
but this text containing test is not a match, only between h tags.
有人能帮我吗?
我有一些类似的东西,它正在测试我是否有任何img标签包含一个带关键字的alt标签,这是我的代码:
$dom = new DOMDocument();
$dom->loadHTML($row['content']);
foreach ($dom->getElementsByTagName('img') as $img) {
$alt = $img->getAttribute('alt');
if (preg_match("/'b".$row_meta['keyword']."'b/", $alt)) {
$counter++;
}
}
不确定这是否正确(我已经很久没有做php了,也不确定这是不是正确的解析器),但我认为你可以做以下
$dom = new DOMDocument();
$dom->loadHTML($row['content']);
foreach ($dom->getElementsByTagName('h2') as $h2) {
$text = $h2->nodeValue;
if (preg_match("/'b".$row_meta['keyword']."'b/", $text)) {
$counter++;
}
}