正则表达式替换 <或>与 >或< html 标签内
例如。
<html>
<head></head>
<body>
<div>
<h1>-----> hello! ----< </h1>
</div>
</body>
我想替换 >且< h1 标签内有相应的 >且<
哪个是正确的模式?
提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与评论者“为什么首先生成这个损坏的 HTML?”一致,如果您表示这样的文档,那么您将遇到当前遇到的这些问题。有两种有效情况
因此,当您从源数据(字符串、数据库)你需要对它们进行转义(例如,通过使用 htmlspecialchars 作为另一个回答者正确指出。)
您需要不惜一切代价避免出现这样的情况:您拥有像您这样的字符串,其中包含 HTML 标签和非转义文本。
例如,如果您的文本包含文本
text
并且您确实希望该文本显示在 HTML 文档中,即您希望看到尖括号而不是文本以粗体显示(例如,您正在编写有关如何对 HTML 进行编程的文档),那么一旦您拥有这样的文档,您就无法将其与实际的 HTML 代码区分开来。In agreement with the commenter "Why is this broken HTML being generated in the first place?", if you represent documents like this then you will have exactly these problems that you are currently having. There are two valid situations
So when you generate the HTML document from your source data (strings, database) you need to do the escaping them (e.g. by using htmlspecialchars as another answerer correctly pointed out.)
You need to avoid, at all costs, a situation where you have a string like you have, which has HTML tags and non-escaped text.
For example, if you text contained the text
<b>text</b>
and you literally wanted that text to be displayed in the HTML document i.e. you wanted the angle-brackets to be seen rather than the text be in bold (e.g. you were writing a document about how to program HTML) then you have no way to differentiate that from actual HTML code once you have such a document.您可以将其扔到
tidy
(请参阅文档< /a>) 并查看是否可以修复错误。比尝试使用正则表达式自己做“正确的事情”要好得多。您可能必须首先在 PHP 环境中启用 tidy。
You could throw it at
tidy
(see the docs) and see if it can fix the errors. A lot better than trying to do the "right thing" on your own with regex.It might be that you must enable tidy first in your PHP environment.
我会通过 tidy 传递它。
I would pass it through tidy.