php實現網頁HTML標籤補全方法

來源:互聯網
上載者:User

如果你的網頁內容的html標籤顯示不全,有些表格標籤不完整而導致頁面混亂,或者把你的內容之外的局部html頁面給包含進去了,我們可以寫個函數方法來補全html標籤以及過濾掉無用的html標籤.

php實現網頁HTML標籤補全方法

php使HTML標籤自動補全閉合函數方法如下:


function closetags($html) {

preg_match_all('#<(?!metaimgbrhrinput\b)\b([a-z]+)(?: .*)?(?#iU', $html, $result);

$openedtags = $result[1];

preg_match_all('##iU', $html, $result);

$closedtags = $result[1];

$len_opened = count($openedtags);

if (count($closedtags) == $len_opened) {

return $html;

}

$openedtags = array_reverse($openedtags);

for ($i=0; $i < $len_opened; $i++) {

if (!in_array($openedtags[$i], $closedtags)) {

$html .= '';

} else {

unset($closedtags[array_search($openedtags[$i], $closedtags)]);

}

}

return $html;

}

解析: 

array_reverse() : 此函數將原數組中的元素順序翻轉,建立新的數組並返回。如果第二個參數指定為 true,則元素的鍵名保持不變,否則鍵名將丟失。

array_search() : array_search(value,array,strict),此函數與in_array()一樣在數組中尋找一個鍵值。如果找到了該值,匹配元素的鍵名會被返回。如果沒找到,則返回 false。 如果第三個參數strict被指定為 true,則只有在資料類型和值都一致時才返回相應元素的鍵名。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.