現在,下面的代碼對html文檔執行測試,看看是否有h1或h2標籤包含字符串$ title。代碼完美無瑕。
$s1='random text';
$a1='random anchor text';
$href1='http://www.someurl.com';
$document = new DOMDocument();
$libxml_previous_state = libxml_use_internal_errors(true);
$document->loadHTML($str);
libxml_use_internal_errors($libxml_previous_state);
$tags = array ('h1', 'h2');
$texts = array();
foreach($tags as $tag)
{
$elementList = $document->getElementsByTagName($tag);
foreach($elementList as $element)
{
$texts[$element->tagName] = strtolower($element->textContent);
}
}
if(in_array(strtolower($title),$texts)) {
echo '<div class="success"><i class="fa fa-check-square-o" style="color:green"></i> This article used the correct title tag.</div>';
} else {
echo '<div class="error"><i class="fa fa-times-circle-o" style="color:red"></i> This article did not use the correct title tag.</div>';
}
我需要運行三個測試,首先我需要掃描的$ s1的存在的文件,但不知道這一點。通過工作代碼,它正在尋找h1或h2標籤內的完全匹配。然而,對於$ s1我不尋找完全匹配,只是在任何文本存在的地方 - 無論是否包含其他文本。
然後,我需要另一個精確匹配測試來查找「a」文本中的$ a1,並且還需要測試href是否存在$ href1。
我不知道如何做這些測試。我相信我可以將$ a1測試看作是另一個精確匹配,但不知道如何執行href測試,也不能掃描可能被其他文本包圍的字符串。
希望這一切都有道理。
更新
我需要一個解決方案,讓我回顯一個單一「是字符串是否存在」或「不,它不需要」。類似於當前測試回聲的唯一方式,而不是每個迴路。我需要每次測試都做一次。
示例結果會是什麼樣子:
yes $s1 is in the document
no $s1 is not in the document
yes $href1 is an href in the document
no $href1 is not an href in the document
yes $a1 is an anchor text in the document
no $a1 is not an anchor text in the document
我也相信我應該使用SUBSTR(),但我不知道究竟如何。
希望得到一些工作實例和詳細解釋。
一些提示:1,你可以用'的foreach($文檔 - >的getElementsByTagName( '*')爲$元素) {'選擇所有元素,然後檢查'$ element-> textContent'作爲你的字符串。 2-請查看[此鏈接](http://www.the-art-of-web.com/php/html-xpath-query/)**第2步**,以瞭解如何尋找你的''標記... – EhsanT
不會在第一個提示中,它只會返回,如果它完全匹配。我的意思是,如果一個元素的內容是「隨機字符串和單詞,然後$ s1和更多的字符和單詞」 – Bruce
所以在這種情況下,你可以使用正則表達式來匹配你的字符串 – EhsanT