使用PHP(ob#u start+;dom解析器)删除HTML

使用PHP(ob#u start+;dom解析器)删除HTML,php,html,ob-start,domparser,Php,Html,Ob Start,Domparser,我需要学习如何使用PHP删除html标记 这就是我的想法(我认为DOM语法是我所需要的,但我不知道它是如何工作的。一个工作示例将对我有很大帮助。我无法安装任何外部库,我正在运行PHP 5): 示例代码: <body> <div class="a"></div> <div id="b"><p class="c">Here are some text and HTML</p></div> <div id="d"

我需要学习如何使用PHP删除html标记

这就是我的想法(我认为DOM语法是我所需要的,但我不知道它是如何工作的。一个工作示例将对我有很大帮助。我无法安装任何外部库,我正在运行PHP 5):

示例代码:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

以下是一些文本和HTML

问题:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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)如何返回:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

以下是一些文本和HTML

2)如何返回:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

3)如何返回:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

以下是一些文本和HTML

下一个示例代码:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

4)如何返回:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

5)如何返回:

<body>
<div class="a"></div>
<div id="b"><p class="c">Here are some text and HTML</p></div>
<div id="d"></div>
</body>
<body>
<p class="c">Here are some text and HTML</p>
</body>
<body>
<div class="a"></div>
<div id="b"></div>
<div id="d"></div>
</body>
<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>
<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>
<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>

感谢阅读:)

尝试使用:

strip_tags();
函数

示例用法

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

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

这将允许“
”标记,并将删除另一个标记。 结果:


下面是一些文本和HTML

更多示例

你可以使用PHP的所有DOM类,你可以在这里查看文档:我相信你可以在你喜欢的地方找到很多教程

    <?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";
    ?>
    <?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";
    ?>
以下是第二个案例的示例:

<?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"));
getElementsByTagName('p')->项(0);
//从DOM中删除p标记
$p->parentNode->removeChild($p);
//保存新的DOM树
$html=$doc->saveXML();
echo$html;
//如果要删除第一行
echo substr($html,strpos($html,“\n”);

试试这个库。它完全满足您的需要,并且有大量关于如何创建过滤器的文档。如果出于安全原因想进行筛选,那么请务必使用它——它有一个解析器,可以处理最疯狂的XSS方案。

为什么不使用strip_tags()方法呢?谢谢您的评论。你能给问题1举个例子吗?看起来他有一些特殊的情况要处理,抱歉,我没注意到你脑子里有具体的部分。您需要DOM解析。但单凭这一点是行不通的。因此,您必须解析所需的位(例如,P标记),并添加其余的程序。我现在知道我必须做什么了。谢谢你,约翰,这不是我想要的。但是谢谢你的回答。你能给我举个例子吗?我觉得这很复杂,我更新了我的帖子。如果你想要更多的细节,那么还有很多其他的例子。