《PHP教程:PHP實(shí)現(xiàn)補(bǔ)齊關(guān)閉的HTML標(biāo)簽》要點(diǎn):
本文介紹了PHP教程:PHP實(shí)現(xiàn)補(bǔ)齊關(guān)閉的HTML標(biāo)簽,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
本文實(shí)例講述了PHP實(shí)現(xiàn)補(bǔ)齊關(guān)閉的HTML標(biāo)簽.分享給大家供大家參考,具體如下:PHP實(shí)例
很多時(shí)候,在我們做文章截取摘要的時(shí)候,如果出現(xiàn)HTML的內(nèi)容,會(huì)出現(xiàn)截取的文章沒有結(jié)束的HTML標(biāo)簽.這樣的情況下就會(huì)出現(xiàn)頁面樣式錯(cuò)亂的問題.這 個(gè)時(shí)候我們需要的就是把缺少的結(jié)束標(biāo)簽加批量加上.在www.php.net官網(wǎng)看到一個(gè)比較好處理的一個(gè)函數(shù),展示如下:PHP實(shí)例
function CloseTags($html) { // strip fraction of open or close tag from end (e.g. if we take first x characters, we might cut off a tag at the end!) $html = preg_replace('/<[^>]*$/','',$html); // ending with fraction of open tag // put open tags into an array preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result); $opentags = $result[1]; // put all closed tags into an array preg_match_all('#</([a-z]+)>#iU', $html, $result); $closetags = $result[1]; $len_opened = count($opentags); // if all tags are closed, we can return if (count($closetags) == $len_opened) { return $html; } // close tags in reverse order that they were opened $opentags = array_reverse($opentags); // self closing tags $sc = array('br','input','img','hr','meta','link'); // ,'frame','iframe','param','area','base','basefont','col' // should not skip tags that can have content inside! for ($i=0; $i < $len_opened; $i++) { $ot = strtolower($opentags[$i]); if (!in_array($opentags[$i], $closetags) && !in_array($ot,$sc)) { $html .= '</'.$opentags[$i].'>'; } else { unset($closetags[array_search($opentags[$i], $closetags)]); } } return $html; }
測(cè)試使用的結(jié)果:PHP實(shí)例
<?php $content = '<div><p><span>越發(fā)忙碌的你,是否想給自己放個(gè)假?專注工作的你,是否還記得上一次熬煉是什么時(shí)候?優(yōu)伴戶外旅行,給你不一樣的旅行體驗(yàn):給心自由,便處處都是風(fēng)景!'; echo CloseTags($content); /* 返回的結(jié)果是: <div><p><span> 越發(fā)忙碌的你,是否想給自己放個(gè)假?專注工作的你,是否還記得上一次熬煉是什么時(shí)候?優(yōu)伴戶外旅行,給你不一樣的旅行體驗(yàn):給心自由,便處處都是風(fēng)景!</span></p></div> */ ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》PHP實(shí)例
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所贊助.PHP實(shí)例
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP教程:PHP實(shí)現(xiàn)補(bǔ)齊關(guān)閉的HTML標(biāo)簽》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7267.html