PHP - 字符串 - 删除具有特定类的 HTML 标记,包括其内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3149682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Strings - Remove a HTML tag with a specific class, including its contents
提问by Alex
I have a string like this:
我有一个这样的字符串:
<div class="container">
<h3 class="hdr"> Text </h3>
<div class="main">
text
<h3> text... </h3>
....
</div>
</div>
how do I remove the H3 tag with the .hdr class using as little code as possible ?
如何使用尽可能少的代码删除带有 .hdr 类的 H3 标签?
回答by Daniel Egeberg
Using as littlecode as possible? Shortest code isn't necessarily best. However, if your HTML h3tag alwayslooks like that, this should suffice:
使用尽可能少的代码?最短的代码不一定是最好的。但是,如果您的 HTMLh3标记始终是这样的,那么这应该就足够了:
$html = preg_replace('#<h3 class="hdr">(.*?)</h3>#', '', $html);
Generally speaking, using regex for parsing HTML isn't a particularly good idea though.
一般来说,使用正则表达式来解析 HTML 并不是一个特别好的主意。
回答by Webnet
Something like this is what you're looking for...
像这样的东西就是你要找的……
$output = preg_replace("#<h3 class=\"hdr\">(.*?)</h3>#is", "", $input);
Use "is" at the end of the regex because it will cause it to be case insensitive which is more flexible.
在正则表达式的末尾使用“is”,因为它会导致它不区分大小写,从而更加灵活。
回答by AlexanderMP
try a preg_match, then a preg_replace on the following pattern:
尝试使用 preg_match,然后使用以下模式的 preg_replace:
/(<h3
[\s]+
[^>]*?
class=[\"\'][^\"\']*?hdr[^\"\']*?[\"\']
[^>]*?>
[\s\S\d\D\w\W]*?
<\/h3>)/i
It's messy, and it should work fine only if the h3 tag doesn't have inline javascript which might contain sequences that this regular expression will react to. It is far from perfect, but in simple cases where h3 tag is used it should work.
它很混乱,只有当 h3 标签没有内联 javascript 时它才能正常工作,该 javascript 可能包含此正则表达式将对其作出反应的序列。它远非完美,但在使用 h3 标签的简单情况下,它应该可以工作。
Haven't tried it though, might need adjustments.
不过没试过,可能需要调整。
Another way would be to copy that function, use your copy, without the h3, if it's possible.
另一种方法是复制该函数,使用你的副本,如果可能的话,不带 h3。
回答by Hissvard
Stumbled upon this via Google - for anyone else feeling dirty using regex to parse HTML, here's a DOMDocument solution I feel much safer with going:
通过谷歌偶然发现了这一点 - 对于使用正则表达式解析 HTML 的其他人来说,这是一个 DOMDocument 解决方案,我觉得更安全:
function removeTagByClass(string $html, string $className) {
$dom = new \DOMDocument();
$dom->loadHTML($html);
$finder = new \DOMXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' {$className} ')]");
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
return $dom->saveHTML();
}
Thanks to this other answerfor the XPath query.
感谢XPath 查询的其他答案。
回答by Jasmeen
$content = preg_replace('~(.*?)~', '', $content);
$content = preg_replace('~(.*?)~', '', $content);
Above code only works if the div haves are both on the same line. what if they aren't?
上面的代码只有在 div 都在同一行时才有效。如果不是呢?
$content = preg_replace('~[^|]*?~', '', $content);
$content = preg_replace('~[^|]*?~', '', $content);
This works even if there is a line break in between but fails if the not so used | symbol is in between anyone know a better way?
即使中间有换行符,这也有效,但如果不这么使用,则失败 | 符号介于两者之间有人知道更好的方法吗?
回答by Jai
This would help someone if above solutions dont work. It remove iframe and content having tag '-webkit-overflow-scrolling: touch;' like i had :)
如果上述解决方案不起作用,这将对某人有所帮助。它删除 iframe 和带有标签“-webkit-overflow-scrolling: touch;”的内容 就像我一样:)
RegEx, or regular expressions is code for what you would like to remove, and PHP function preg_replace() will remove all div or divs matching, or replacing them with something else. In the examples below, $incoming_data is where you put all your content before removing elements, and $result is the final product. Basically we are telling the code to find all divs with class=”myclass” and replace them with ” ” (nothing).
RegEx 或正则表达式是您要删除的代码,PHP 函数 preg_replace() 将删除所有匹配的 div 或 div,或用其他内容替换它们。在下面的示例中,$incoming_data 是您在删除元素之前放置所有内容的位置,而 $result 是最终产品。基本上,我们告诉代码找到所有 class=”myclass” 的 div 并将它们替换为“”(什么都没有)。
How to remove a div and its contents by class in PHP Just change “myclass” to whatever class your div has.
如何在 PHP 中按类删除 div 及其内容只需将“myclass”更改为您的 div 具有的任何类。
$result = preg_replace('#<div class="myclass">(.*?)</div>#', ' ',
$incoming_data);
How to remove a div and its contents by ID in PHP Just change “myid” to whatever ID your div has.
如何在 PHP 中通过 ID 删除 div 及其内容只需将“myid”更改为您的 div 具有的任何 ID。
$result = preg_replace('#(.*?)#', ' ', $incoming_data);
$result = preg_replace('#(.*?)#', ' ', $incoming_data);
If your div has multiple classes? Just change “myid” to whatever ID your div has like this.
如果你的 div 有多个类?只需将“myid”更改为您的 div 具有的任何 ID,就像这样。
$result = preg_replace('#<div id="myid(.*?)</div>#', ' ', $incoming_data);
or if div don't have an ID, filter on the first class of the div like this.
$result = preg_replace('#<div class="myclass(.*?)</div>#', ' ', $incoming_data);
How to remove all headings in PHP This is how to remove all headings.
如何在 PHP 中删除所有标题 这是删除所有标题的方法。
$result = preg_replace('#<h1>(.*?)</h1>#', ' ', $incoming_data);
and if the heading have a class, do something like this:
$result = preg_replace('#<h1 class="myclass">(.*?)</h1>#', ' ', $incoming_data);
来源:http: //www.lets-develop.com/html5-html-css-css3-php-wordpress-jquery-javascript-photoshop-illustrator-flash-tutorial/php-programming/remove-div-by-class- php-remove-div-contents/

