basballguy Posted March 8, 2011 Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/ Share on other sites More sharing options...
salathe Posted March 8, 2011 Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/#findComment-1184442 Share on other sites More sharing options...
basballguy Posted March 8, 2011 Author Share Posted March 8, 2011 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 }? Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/#findComment-1184564 Share on other sites More sharing options...
salathe Posted March 8, 2011 Share Posted March 8, 2011 The closest that you'll be able to get (simply) with DOM would be to get the textContent of the appropriate <script> element. That'll be a good start at least. Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/#findComment-1184572 Share on other sites More sharing options...
basballguy Posted March 8, 2011 Author Share Posted March 8, 2011 thanks...i've taken your suggestions and i'm on the right path. I think i can get it from here. appreciate the feedback Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/#findComment-1184651 Share on other sites More sharing options...
basballguy Posted March 8, 2011 Author Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229929-domdocument-parsing-obstacle/#findComment-1184663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.