hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP, Tidy, and parsing through trees
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP, Tidy, and parsing through trees

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 08-24-2006, 05:07 PM
Adam Hallett Adam Hallett is offline
WHT Addict
 
Join Date: Aug 2001
Posts: 123

PHP, Tidy, and parsing through trees


As you may know Tidy is a tool that cleans up html. It has a binding for php. One of the things you can do with Tidy is generate a parse tree. This tree contains arrays, which give way to objects. Here is the output of var_dump().

http://www.harddrivecaddy.com/temp/tidy.node.output.txt

I want to create a class that provides back/forward navigation (among other thigns) through the tree but I'm unsure how to write that functionality. Not only do I need to go through all the arrays and objects, but I also need to go through all the childs.

Reply With Quote


Sponsored Links
  #2  
Old 08-25-2006, 10:03 AM
sea otter sea otter is offline
the cloud is a lie
 
Join Date: May 2004
Location: NYC
Posts: 793
From a (quick) look at it, I think tidy is return the information in serialized format, so you need to do this:

Code:
$str = get_tidy_data();  // or whatever you do...
$results = unserialize($str);
It seems that the unserialzed object will be a numerically-indexed array with two elements. Once you've unserialized the data, you can take a quick look with

Code:
echo '<pre>';
print_r($results);
echo '</pre>';
The PHP manual page for unserialize is here: http://us3.php.net/manual/en/function.unserialize.php

As for traversal, well, at that point it's like traversing any array in PHP.

Alternatively, if you want to provide a fancy interactive Javascript tree browser, you might want to see if tidy can return the data in JSON format, or if there's a php-serialized-to-json converter out there (if not, it's a simple two-step affair to go from the unserialized array to JSON).

The JSON page is here http://json.org/

Reply With Quote
  #3  
Old 08-25-2006, 12:53 PM
Adam Hallett Adam Hallett is offline
WHT Addict
 
Join Date: Aug 2001
Posts: 123
Quote:
$tidy = new tidy;
$tidy->parseString($file, $config, 'utf8');
$tree = tidy_get_root($tidy);
This produces an array. So there is no need to unserialize() the object. I'm looking an algorithim to go through all arrays that navigates through objects to find more arrays.

for example

$tmp = &$tree[0];

if ( $tmp->isChild() ) {
//now we've found an array, possibly a collection of arrays!

}

Reply With Quote
Sponsored Links
  #4  
Old 08-25-2006, 02:44 PM
sea otter sea otter is offline
the cloud is a lie
 
Join Date: May 2004
Location: NYC
Posts: 793
Ah...we're not dealing with regular arrays here. There's an example function to do what you want on the PHP documentation page:

http://us3.php.net/tidy_get_root

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Alert Logic Releases New Version of Security and Compliance Solution Web Hosting News 2013-02-19 15:55:16
Ruby on Rails Vulnerabilities Discovered, Users Urged to Update Immediately Web Hosting News 2013-01-10 11:05:11
Web Drive Acquires New Zealand Hosting Rival FlexiHost Web Hosting News 2012-07-31 10:19:06
Data Storage Corp. Partners with Document Automation Solutions Firm inFORM Decisions Web Hosting News 2012-01-05 16:26:16
Join us in Scottsdale, AZ tonight for the WHIR Networking Event Blog 2011-09-22 17:03:41


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?