如何从 HTML 标签中去除样式属性?
我想用 PHP 删除标签中的所有样式。
例如。
原始:
最终:
这是一个示例:
$body_htm='<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>[Some Title] some text...</title>
</head>
<body style="background-color: #F2F2F2; color: #222; font-family: georgia,serif; letter-spacing: -0.01em; line-height: 1.25; margin-bottom: 0.55em; font-size: 1.2em;">
<div style="background-color: #F2F2F2; border: 2px dotted #333; padding: 55px 0 55px 55px;">
<div style="background-color: #F2F2F2; width: 400px;">
<p style="margin-bottom:110px;"><b>Hello!!!</b></p>';
它应该返回:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>[Some Title] some text...</title>
</head>
<body>
<div>
<div>
<p><b>Hello!!!</b></p>';
有什么想法吗?
I want to remove all styles from tags with PHP.
For example.
Original:
<body style="color:back;">
Final:
<body>
Here's a example:
$body_htm='<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>[Some Title] some text...</title>
</head>
<body style="background-color: #F2F2F2; color: #222; font-family: georgia,serif; letter-spacing: -0.01em; line-height: 1.25; margin-bottom: 0.55em; font-size: 1.2em;">
<div style="background-color: #F2F2F2; border: 2px dotted #333; padding: 55px 0 55px 55px;">
<div style="background-color: #F2F2F2; width: 400px;">
<p style="margin-bottom:110px;"><b>Hello!!!</b></p>';
It should return this:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>[Some Title] some text...</title>
</head>
<body>
<div>
<div>
<p><b>Hello!!!</b></p>';
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个非常简单的替换可能会做:
希望这有帮助
A very simple replace will probably do:
Hope this helps
如果您无法阻止
style
属性首先被插入,我建议使用类似 的内容HTML 净化器。它是针对此类问题的成熟解决方案,并且还允许您在将来以最小的努力执行更多 HTML 过滤(例如 XSS 预防)。使用正则表达式解决方案可能会产生问题,通常需要越来越复杂的正则表达式来纠正,直到您自己重新创建了类似 HTML Purifier 之类的东西(并在这个过程中失去了理智)。如果您收到无效的标记,使用 DOM 扩展也会产生问题。虽然 HTML Purifier 可能在各方面都不是完美的,但它会满足您的需要并受到支持。
If you can't stop the
style
attributes from being inserted in the first place, I'd suggest something like HTML Purifier. It's a well developed solution for exactly this kind of problem, and also allows you to perform more HTML filtering in the future (XSS prevention, for instance) with minimum effort.Problems can be created by using a regular expression solution, usually necessitating more and more complex regular expressions to rectify until you've essentially recreated something like HTML Purifier yourself (and lost your mind in the process). Using the DOM extension can also create problems if you are handed invalid markup. While HTML Purifier is probably not perfect in every way, it will do what you need and is supported.
你不能修改输出,即不包含样式标签(内联样式无论如何都是不好的做法)吗?
是否可以包含您自己的 CSS 并使用
!important
规则覆盖 body 属性来修改您的 输出?如果两个问题的答案都是“否”,则使用正则表达式或 DOM/XML-Parser 来删除它。
Cant you modify the output, that the style-tag is not included (inline-styles are anyway bad practice)?
Is it possible to include your own CSS and override the body properties with an
!important
rule to modify your output?If answered both question with "no", then use a regex or a DOM/XML-Parser to remove it.