使用 PHP 删除 HTML(ob_start + dom 解析器)

发布于 2024-10-31 04:02:37 字数 2077 浏览 4 评论 0原文

我需要学习如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-11-07 04:02:37

尝试使用 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.

大海や 2024-11-07 04:02:37

您可以使用 PHP 的所有 DOM 类,您将在此处查看文档: https: //www.php.net/manual/en/book.dom.php 我确信您可以找到很多您喜欢的教程。

这是第二种情况的示例:

<?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"));

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
$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"));
输什么也不输骨气 2024-11-07 04:02:37

尝试使用

strip_tags();

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>
Here are some text and HTML
</body>

更多示例 PHP.Net

Try to use :

strip_tags();

function in php.

Sample Usage:

    <?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";
    ?>

it will return :

Here are some text and HTML 

or:

    <?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";
    ?>

this will allow '<body>' tag and will remve another tags.
result :

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

More Examples:Php.Net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文