使用 PHP 删除 HTML(ob_start + dom 解析器)
我需要学习如何使用 PHP 删除 html 标签。
这就是我的想法(我认为 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>
感谢您的阅读:)
I need to learn how to remove html tags using PHP.
This is what I have in mind (I think DOM phrasing is what I need but I cant figure out how it works. A working example would be a big help for me. I can't install any external library’s and I am running PHP 5):
function the_remove_function($remove){
// dom parser code here?
return $remove;}
// return all content into a string
ob_start('the_remove_function');
Example code:
<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
Questions:
1) How do I return:
<body>
<p class="c">Here are some text and HTML</p>
</body>
2) How do I return:
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
3) How do I return:
<body>
<div class="a"></div>
<p class="c">Here are some text and HTML</p>
<div id="d"></div>
</body>
Next example code:
<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) How do I return:
<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) How do I return:
<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>
Thanks for reading :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 HTML Purifier 库。它完全满足您的需要,并且有关于如何创建过滤器的大量文档。如果您出于安全原因想要进行过滤,那么请务必使用它 - 它有一个解析器,可以应对可以想象到的最疯狂的 XSS 方案。
Try the HTML Purifier library. It does exactly what you need and has extensive documentation on how to create filters. If you want to filter because of security reasons, then by all means use it - it has a parser that can cope with the most crazy XSS schemes imaginable.
您可以使用 PHP 的所有 DOM 类,您将在此处查看文档: https: //www.php.net/manual/en/book.dom.php 我确信您可以找到很多您喜欢的教程。
这是第二种情况的示例:
You can use all the DOM classes of PHP, you will the doc here : https://www.php.net/manual/en/book.dom.php and I'm sur you can find a lot of tutorial in you prefer.
Here is an exemple for your second case :
尝试使用
php 中的 : 函数。
示例用法:
它将返回:
或:
这将允许“
”标签并删除其他标签。
结果:
更多示例: PHP.Net
Try to use :
function in php.
Sample Usage:
it will return :
or:
this will allow '
<body>
' tag and will remve another tags.result :
More Examples:Php.Net