溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP怎么實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾

發(fā)布時間:2022-04-26 16:32:54 來源:億速云 閱讀:129 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“PHP怎么實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“PHP怎么實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾”吧!

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

php使HTML標(biāo)簽自動補全,閉合,過濾函數(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)簽自動補全,閉合,過濾函數(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ù)。

感謝各位的閱讀,以上就是“PHP怎么實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對PHP怎么實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI