如何关闭未关闭的HTML标签? [英] How to close unclosed HTML Tags?

查看:671
本文介绍了如何关闭未关闭的HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我们通过数据库或类似来源的某些编辑获取一些用户输入的内容时,我们可能会检索仅包含开始标记但没有关闭的部分。

Whenever we are fetching some user inputed content with some editing from the database or similar sources, we might retrieve the portion which only contains the opening tag but no closing.

这可能会妨碍网站的当前布局。

This can hamper the website's current layout.

是否有客户端或服务器端方式解决此问题?

Is there a clientside or serverside way of fixing this?

推荐答案

找到了一个很好的答案:

Found a great answer for this one:

使用PHP 5并使用DOMDocument对象的loadHTML()方法。这自动解析格式错误的HTML,随后调用saveXML()将输出有效的HTML。 DOM函数可以在这里找到:

Use PHP 5 and use the loadHTML() method of the DOMDocument object. This auto parses badly formed HTML and a subsequent call to saveXML() will output the valid HTML. The DOM functions can be found here:

http://www.php.net / dom

使用此:

$doc = new DOMDocument();
$doc->loadHTML($yourText);
$yourText = $doc->saveHTML();

这篇关于如何关闭未关闭的HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆