PHP移除指定HTML標籤方法總結

來源:互聯網
上載者:User

有時候我們需要把html標籤頁存到資料庫裡,但是有些場合卻需要拿無html標籤的純資料,這個時候就要對帶html標籤的資料進行處理,把html標籤都去掉。平時用 htmlspecialchars() 來過濾html,但是把html的字元轉義了,最後顯示出來的就是html原始碼,利用strip_tags()就可以把html標籤去除掉。


PHP預設的函數有移除指定html標籤,名稱為strip_tags,在某些場合非常有用。

strip_tags


strip_tags — Strip HTML and PHP tags from a string


string strip_tags ( string str [, string allowable_tags] )

弊端 :


這個函數只能保留想要的html標籤,就是參數string allowable_tags。

這個函數的參數allowable_tags的其他的用法。

 代碼如下 複製代碼


strip_tags($source, ”); 去掉所以的html標籤。
strip_tags($source, ‘<div><img><em>’); 保留字元串中的div、img、em標籤。

如果想去掉的html的指定標籤。那麼這個函數就不能滿足需求了。


於是乎我用到了這個函數。

 代碼如下 複製代碼

function strip_only_tags($str, $tags, $stripContent = FALSE) {
  $content = '';
 
  if (!is_array($tags)) {
    $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
    if (end($tags) == '') {
      array_pop($tags);
    }
  }
 
  foreach($tags as $tag) {
    if ($stripContent) {
      $content = '(.+<!--'.$tag.'(-->|s[^>]*>)|)';
    }
 
    $str = preg_replace('#<!--?'.$tag.'(-->|s[^>]*>)'.$content.'#is', '', $str);
  }
 
  return $str;
}


參數說明


$str  — 是指需要過濾的一段字串,比如div、p、em、img等html標籤。
$tags — 是指想要移除指定的html標籤,比如a、img、p等。
$stripContent = FALSE  — 移除標籤內的內容,比如將整個連結刪除等,預設為False,即不刪除標籤內的內容。

使用說明

 代碼如下 複製代碼


$target = strip_only_tags($source, array(‘a’,'em’,'b’));移除$source字串內的a、em、b標籤。

$source='<div><a href="" target="_blank"><img src="logo.png" border="0" alt="Welcome to linzl." />This a example from<em>lixiphp</em></a><strong>!</strong></div>
 ';
 
$target = strip_only_tags($source, array('a','em'));
 
//target results
//<div><img src="/logo.png" border="0" alt="Welcome to lixiphp." />This a example from<strong>!</strong></div>

其它辦法

 代碼如下 複製代碼


<?php
//取出br標記
function strip($str)
{
$str=str_replace("<br>","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>


一個自訂的函數

/

 代碼如下 複製代碼
**
 * 取出html標籤
 *
 * @access public
 * @param string str
 * @return string
 *
 */
function deletehtml($str) {
    $str = trim($str); //清除字串兩邊的空格
    $str = strip_tags($str,"<p>"); //利用php內建的函數清除html格式。保留P標籤
    $str = preg_replace("/t/","",$str); //使用Regex匹配需要替換的內容,如:空格,換行,並將替換為空白。
    $str = preg_replace("/rn/","",$str);
    $str = preg_replace("/r/","",$str);
    $str = preg_replace("/n/","",$str);
    $str = preg_replace("/ /","",$str);
    $str = preg_replace("/  /","",$str);  //匹配html中的空格
    return trim($str); //返回字串
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.