Beun Posted March 3, 2014 Share Posted March 3, 2014 I want to extract the contents of a <td> tag. The fact that this <td> tag has a specific ID, should make it easier, I guess. What I have now is this: $regex = '/<td>([^>]*)<\/td>/'; This of course cathes all <td>'s on the page. And not just the one I want. How can I let regex know I only want the contents of this...? <td id="thisistheone"> Tips/tricks/suggestions? Thx Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/ Share on other sites More sharing options...
kicken Posted March 3, 2014 Share Posted March 3, 2014 Don't use regex at all for this. Use the DOMDocument extension to parse the HTML and extract the contents you need. <?php $html = '<html><head><title>blah</title></head><body> <table><tr><td id="blah">the content</td></tr></table> </body></html>'; $doc = new DOMDocument(); $doc->loadHTML($html); $td = $doc->getElementById('blah'); if ($td){ echo $td->textContent; } else { echo 'TD Not found'; } Depending on what the content is that you want, you may need to do more than just access the textContent property. There are a number of different ways to get at the content. Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/#findComment-1471314 Share on other sites More sharing options...
Beun Posted March 3, 2014 Author Share Posted March 3, 2014 Wow. Thanks. However... Even though I am getting the correct/expected outcome, above those results, I am also getting some error messages. Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID ID-IP already defined in Entity, line: 144 in /home/.../public_html/3/temp1.php on line 6 Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID tituloTABLA2 already defined in Entity, line: 151 in /home/.../public_html/3/temp1.php on line 6 Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID ID-IP already defined in Entity, line: 154 in /home/.../public_html/3/temp1.php on line 6 Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Unexpected end tag : form in Entity, line: 294 in /home/.../public_html/3/temp1.php on line 6 Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Unexpected end tag : form in Entity, line: 296 in /home/.../public_html/3/temp1.php on line 6 Does the following seem correct? <?php $html = file_get_contents('http://url/file.php'); $doc = new DOMDocument(); $doc->loadHTML($html); $td = $doc->getElementById('thisistheone'); if ($td){ echo $td->textContent; } else { echo 'TD Not found'; } ?> (FWIW... What I have here are several hardware boxes, each and every one with its own little webser/webpage. I need to grab/collect specific information from all of those pages, and display them on one page. One page to display them all, so to speak Thx. Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/#findComment-1471316 Share on other sites More sharing options...
ginerjm Posted March 3, 2014 Share Posted March 3, 2014 Out of curiosity, why do you need to get something from a page this way? Why not just output the specific value in an <input> tag that is stylized so as to appear as plain text (if necessary) and have the id on that? Wrap the table inside the form tag and let it come to your php script as part of the $_POST array. Then process it. Or write a JS function to process the id contents on submit and let the JS place the data into a hidden input element, to then be picked up again in the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/#findComment-1471320 Share on other sites More sharing options...
Beun Posted March 3, 2014 Author Share Posted March 3, 2014 Out of curiosity, why do you need to get something from a page this way? Why not just output the specific value in an <input> tag that is stylized so as to appear as plain text (if necessary) and have the id on that? Wrap the table inside the form tag and let it come to your php script as part of the $_POST array. Then process it. Or write a JS function to process the id contents on submit and let the JS place the data into a hidden input element, to then be picked up again in the $_POST array. Why not, you ask. Well, because I am somewhat of a PHP n00b What I started this topic with is kind of what I know about how to get content from another page. About what you propose; I see what you mean, and I understand that could indeed work, but I do not know how to code that Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/#findComment-1471324 Share on other sites More sharing options...
ginerjm Posted March 3, 2014 Share Posted March 3, 2014 Generally you get content from a page via a POST or GET that sends that page's input elems to the receiving script. That script takes its input and decides what to do with it, such as posting a db or making some decision as to what 'page' to send back to the client. You should really do some reading on how web apps work and how to navigate between the server and the client (user) and back. Forms and their input elements (tags) are a major part of the communication process. Quote Link to comment https://forums.phpfreaks.com/topic/286673-extract-contents-of-tag-with-having-specific-id/#findComment-1471335 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.