最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補(bǔ)全和過濾的方法小結(jié)【2種方法】

 更新時間:2017年04月27日 10:58:53   作者:websites  
這篇文章主要介紹了PHP實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補(bǔ)全和過濾的方法,結(jié)合實例形式分析了php常見的標(biāo)簽檢查、補(bǔ)全、閉合、過濾等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補(bǔ)全和過濾的方法。分享給大家供大家參考,具體如下:

如果你的網(wǎng)頁內(nèi)容的html標(biāo)簽顯示不全,有些表格標(biāo)簽不完整而導(dǎo)致頁面混亂,或者把你的內(nèi)容之外的局部html頁面給包含進(jìn)去了,我們可以寫個函數(shù)方法來補(bǔ)全html標(biāo)簽以及過濾掉無用的html標(biāo)簽.

php使HTML標(biāo)簽自動補(bǔ)全,閉合,過濾函數(shù)方法一:

代碼:

function closetags($html) {
 preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
 $openedtags = $result[1];
 preg_match_all('#</([a-z]+)>#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 .= '</'.$openedtags[$i].'>';
    }else {
     unset($closedtags[array_search($openedtags[$i], $closedtags)]);
    }
 }
 return $html;
}

closetags()解析:

array_reverse() : 此函數(shù)將原數(shù)組中的元素順序翻轉(zhuǎn),創(chuàng)建新的數(shù)組并返回。如果第二個參數(shù)指定為 true,則元素的鍵名保持不變,否則鍵名將丟失。

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

php使HTML標(biāo)簽自動補(bǔ)全,閉合,過濾函數(shù)方法二:

function checkhtml($html) {
  $html = stripslashes($html);
    preg_match_all("/\<([^\<]+)\>/is", $html, $ms);
    $searchs[] = '<';
    $replaces[] = '<';
    $searchs[] = '>';
    $replaces[] = '>';
    if($ms[1]) {
      $allowtags = 'img|font|div|table|tbody|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li';//允許的標(biāo)簽
      $ms[1] = array_unique($ms[1]);
      foreach ($ms[1] as $value) {
        $searchs[] = "<".$value.">";
        $value = shtmlspecialchars($value);
        $value = str_replace(array('\\','/*'), array('.','/.'), $value);
        $value = preg_replace(array("/(javascript|script|eval|behaviour|expression)/i", "/(\s+|"|')on/i"), array('.', ' .'), $value);
        if(!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) {
          $value = '';
        }
        $replaces[] = empty($value)?'':"<".str_replace('"', '"', $value).">";
      }
    }
    $html = str_replace($searchs, $replaces, $html);
  return $html;
}
//取消HTML代碼
function shtmlspecialchars($string) {
  if(is_array($string)) {
    foreach($string as $key => $val) {
      $string[$key] = shtmlspecialchars($val);
    }
  } else {
    $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
      str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));
  }
  return $string;
}

checkhtml($html)解析:

stripslashes():函數(shù)刪除由addslashes()函數(shù)添加的反斜杠。該函數(shù)用于清理從數(shù)據(jù)庫或HTML表單中取回的數(shù)據(jù)。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

福建省| 陇西县| 龙山县| 海淀区| 翁牛特旗| 青州市| 永平县| 习水县| 绥江县| 巨鹿县| 宁蒗| 华亭县| 鄢陵县| 开封市| 武义县| 肥乡县| 松潘县| 东至县| 蓝田县| 中江县| 长海县| 天峨县| 宁城县| 耒阳市| 山阴县| 沂源县| 巴彦淖尔市| 齐齐哈尔市| 沂南县| 尖扎县| 开远市| 宣恩县| 聂荣县| 武清区| 扬中市| 南丰县| 外汇| 涿州市| 中方县| 西和县| 临泽县|