Jump to content

domdocument parsing obstacle


basballguy

Recommended Posts

so there's some html i'm having to fetch and parse for personal use...but some of the data i want is started in a table written this way:

 

rawTableData = {"rows": [{"colname1": value, "colname2": "value", "colname3: value}

 

how can i use domdocument to parse this data if say i want the value for colname2?  there are no tags for me to use. 

Link to comment
Share on other sites

It looks like you want to parse a piece of (broken) JavaScript text within the HTML.

 

You could use DOM to get get at the block of JavaScript code, then use string functions go narrow it down to the particular piece of code you are interested in.

 

Optionally you could then use something like json_decode() to parse that (broken) JSON (the {...} part) into PHP values, or keep going with the string functions.

Link to comment
Share on other sites

thanks for the followup....i only put the first row of data in my post to give the reader an idea of what i was looking at.  Not to familiar with JSON but it looks like i can use it since that's the format represented.

 

 

so in the example above...assuming it was a complete data set, lets talk about how i'd approach this cause right now my experience is only with using domelement functions. 

 

Step 1) Get block of code

Step 2) assign rawTabledata as a variable

Step 3) use JSON to interpret rawTabledata into an array

Step 4) Win

 

So in looking over dom, i'm not sure which function i can use to get the entire block of text..and is that what i'm doing?  the entire thing from the first letter in the line to the last }?

 

 

Link to comment
Share on other sites

without posting a new thread though, i don't think i understand enough about the dom functionality.

 

My other scenario is

 

<body>
<table class='garbage'>
<tr>
<td>color</td>
<td>size</td>
<td>shape</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
<table class='stuff'>
<tr>
<td>name</td>
<td>address</td>
<td>city</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>

 

So this is what the html could hypothetically look like. 

 

I want to completely ignore the first table and start collecting data in the second table and i'm trying to do it like this:

(Assumption:  doc is already loaded)

 


$dashboard = $doc->getElmentbyTagName('Table');
$char = $dashboard->item(0)->getElementbyTagName('tr');

     foreach ($char as $c) {
        assign element 1
        assign element 2
        assign element 3
      }

 

so in the second piece of code, i will go through both tables when i only want to go through the second table.  My question is how do i skip the first table to ensure i'm only getting data out of the second table....and third and forth table....so only skipping the first one. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.