PHP中的DOM操作 [英] DOM manipulation in PHP

查看:78
本文介绍了PHP中的DOM操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在PHP中操纵HTML的好方法。例如,我目前遇到的问题是处理格式错误的html。

I am looking for good methods of manipulating HTML in PHP. For example, the problem I'm currently have is dealing with malformed html.

我正在获得如下输入:

<div>This is some <b>text

如你所知,html缺少关闭标签。我可以使用正则表达式或XML解析器来解决这个问题。但是,将来我可能会做其他的DOM操作。我想知道是否有任何好的PHP库处理类似于JavaScript处理DOM操作的DOM操作。

As you noticed, the html is missing closing tags. I could use regex or a XML Parser to solve this problem. However, it is likely that I will have to do other DOM manipulation in the future. I am wonder if there is any good PHP libraries that handle DOM manipulation similar to how Javascript deals with DOM manipulation.

推荐答案

PHP有一个允许您访问HTML Tidy功能的PECL扩展程序。 Tidy是一个非常强大的图书馆,应该能够采用这样的代码,并以智能的方式关闭标签。

PHP has a PECL extension that gives you access to the features of HTML Tidy. Tidy is a pretty powerful library that should be able to take code like that and close tags in an intelligent manner.

我用它来清理格式错误的XML和HTML发送到导入之前由我的分类广告系统。

I use it to clean up malformed XML and HTML sent to me by a classified ad system prior to import.

这篇关于PHP中的DOM操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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