2011-04-10 89 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方案。