Jump to content

Extract contents of <td> tag, with <td> having specific ID.


Beun

Recommended Posts

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 :)

 

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :unsure: 

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 :)

Link to comment
Share on other sites

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.

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.