2011-04-10 86 views
2

我需要學習如何使用PHP刪除html標籤。使用PHP刪除HTML(ob_start + dom解析器)

這是我的想法(我認爲DOM的措辭是我需要的,但我無法弄清它是如何工作的,一個可行的例子對我來說是一個很大的幫助,我不能安裝任何外部庫和我正在運行PHP 5):

function the_remove_function($remove){ 

// dom parser code here? 

return $remove;} 

// return all content into a string 
ob_start('the_remove_function'); 

示例代碼:

<body> 
<div class="a"></div> 
<div id="b"><p class="c">Here are some text and HTML</p></div> 
<div id="d"></div> 
</body> 

問題:

1)如何退還:

<body> 
<p class="c">Here are some text and HTML</p> 
</body> 

2)如何退還:

<body> 
<div class="a"></div> 
<div id="b"></div> 
<div id="d"></div> 
</body> 

3)如何返回:

<body> 
<div class="a"></div> 
<p class="c">Here are some text and HTML</p> 
<div id="d"></div> 
</body> 

下一個示例代碼:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel='stylesheet' id='test-css' href='http://www.domain.com/css/test.css?ver=2011' type='text/css' media='all' /> 
<script type='text/javascript' src='http://www.domain.com/js/test.js?ver=2010123'></script> 
</head> 

4)如何退還:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel='stylesheet' id='test-css' href='http://www.domain.com/css/test.css?ver=2011' type='text/css' media='all' /> 
</head> 

5)如何退還:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script type='text/javascript' src='http://www.domain.com/js/test.js?ver=2010123'></script> 
</head> 

感謝您的閱讀:)

+1

爲什麼不只是使用strip_tags()方法? – JohnP 2011-04-10 09:25:05

+0

感謝您的評論。你能舉一個問題1的例子嗎? – Hakan 2011-04-10 09:28:00

+0

看起來像他有一些特殊情況下刪除中間和東西 – tradyblix 2011-04-10 09:28:27

回答

1

您可以使用PHP的所有DOM類,你會在這裏的DOC:http://fr2.php.net/manual/en/book.dom.php,我SUR你可以找到很多的在你喜歡的教程。

這裏是你的第二個案例的爲例:

<?php 
$content = '<body><div class="a"></div><div id="b"><p class="c">Here are some text and HTML</p></div><div id="d"></div></body>'; 
$doc = new DOMDocument(); 
$doc->loadXML($content); 

//Get your p element 
$p = $doc->getElementsByTagName('p')->item(0); 
//Remove the p tag from the DOM 
$p->parentNode->removeChild($p); 

//Save you new DOM tree 
$html = $doc->saveXML(); 

echo $html; 
//If you want to delete the first line 
echo substr($html, strpos($html, "\n")); 
+0

你想舉個例子嗎?我認爲這很難學。 – Hakan 2011-04-10 10:30:13

+0

我更新了我的帖子。如果你想要更多的細節搜索,那麼還有其他很多例子。 – TrexXx 2011-04-10 13:23:34

1

嘗試使用:

strip_tags(); 

function in php。

用法示例

<?php 
    $str = '<body> 
      <div class="a"></div> 
      <div id="b"><p class="c">Here are some text and HTML</p></div> 
      <div id="d"></div> 
      </body> 
      '; 
    echo strip_tags($str); 
    echo "\n"; 
    ?> 

則回覆:

Here are some text and HTML 

<?php 
    $str = '<body> 
      <div class="a"></div> 
      <div id="b"><p class="c">Here are some text and HTML</p></div> 
      <div id="d"></div> 
      </body> 
      '; 
    echo strip_tags($str, '<body>'); 
    echo "\n"; 
    ?> 

這將使 '<body>' 標籤,將remve另一個牛逼AGS。 結果:

<body> 
Here are some text and HTML 
</body> 

更多示例Php.Net

+0

不完全是我在找什麼。但謝謝你的回答。 – Hakan 2011-04-10 10:26:19

2

嘗試HTML Purifier庫。它完全符合您的需求,並提供有關如何創建過濾器的大量文檔。如果您想要因安全原因進行過濾,那麼請儘量使用它 - 它有一個解析器,可以應對可以想象的最瘋狂的XSS方案。