Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM manipulation in PHP

Tags:

html

dom

php

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

I am getting input that looks something like this:

<div>This is some <b>text

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

like image 475
Eldila Avatar asked Aug 26 '08 01:08

Eldila


People also ask

Can you do DOM manipulation in PHP?

So if you're ever working with the content for a post (a post type or a custom post type, for that matter) and you need to manipulate tags much like you would with JavaScript, then using the DomDocument library is one of the most powerful tools are your disposal.

What is DOM object in PHP?

A HTML Dom parser written in PHP5. X versions. Dom Parser is very good at dealing with XML as well as HTML. Dom parser travels based on tree based and before access the data, it will load the data into dom object and it will update the data to the web browser.

Which method is used for DOM manipulation?

The jQuery after() method inserts content (new or existing DOM elements) after target element(s) which is specified by a selector.


1 Answers

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.

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

like image 70
ceejayoz Avatar answered Oct 08 '22 06:10

ceejayoz