Forum Moderators: coopster

Message Too Old, No Replies

Using PHP's DOM Functions to Transform <table> into <div> Elements

         

cmarshall

3:03 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Neglected Post [webmasterworld.com]

I've not [yet] delved deeply into XML/XSLT wizardry, but I'm sidling along towards it.

Here's what I consider my first challenge:

I have the output from a Web service that delivers XHTML 1.1-Compliant markup as a set of nested <table> elements. Since it is displaying tabular data, this is perfectly legitimate.

Normally, I embed this in my pages using PHP's include(url) [us3.php.net] function.

What I want to do is convert the <table>,<tr> and <td> elements to <div> elements, and the </table>, </tr> and </td> elements to </div>.

This is so I could reformat the data for cellphones and handhelds.

I could do this with preg_replace() [us3.php.net], but that seems awful "20th Century." Is there a way to do this with PHP's DOM functions [us3.php.net]?

Remember, this needs to be processed HTML output (Rendered URI), not raw source (which is why I use PHP's include(url) function). I believe that you can use the DOM Objects to "suck in" a rendered URI.

cameraman

7:46 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can definitely do it that way, but you're looking at a learning curve - which, from what I've seen of your posts, won't be much of a problem for you. With a proficiency in regex, you could probably get it done in a few minutes (me, I'd agonize for weeks). Going the DOM route means a few hours of study and experimentation. I'm only partway up that hill. When my brain went fuzzy on DOM I moved on to SimpleXML, but it looks to me like that won't work for what you want to do - DOM is the [x]path...

There's definitely nothing wrong with expanding one's horizons, but just because something's 20th century don't mean it's even got a foot in the proverbial pasture.

cmarshall

8:07 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I've been looking for an excuse to learn XML/XSLT stuff, but maybe this isn't it.

It's a total breeze to do it with a preg_replace() and the ob_* functions. I could have it sorted out in half an hour.

Thanks.