Jump to content

Recommended Posts

example this table... then you want to get the content of the TD

 


<table>
    <tr>
        <td id='data'>test</td>
    </tr>
</table>

 

//Javascript
var value = document.getElementById('data');
alert(value);

lets say:

 

the webpage is:

 

<html>

<head>

.......

</head>

<body>

 

something here no need

 

<table>

<tr>

<td>text</td><td>text2</td>

</tr>

<tr>

<td>text3</td><td>text4</td>

</tr>

</table>

</body>

</html>

and I want to get "text" "text2" "text3" "text4" from the table.

 

Thanks!!

Okay then. But there's a reason I asked for the link, 'cause the code depends on exactly what's surrounding the content you're trying to scrape. Here's an example nonetheless, which returns contents inside each td tag:

 

<?php
$string = '<table>
<tr>
<td>text</td><td>text2</td>
</tr>
<tr>
<td>text3</td><td>text4</td>
</tr>
</table>';
//search the string for a pattern, and store the content found inside the set of parens in the array $matches
preg_match_all('|<td>(.*?)</td>|is', $string, $matches);
//see what's inside $matches[1]
echo '<pre>' . print_r($matches[1], true) . '</pre>';
?>

 

To load a website's source code into the $string variable, use

 

<?php
$url = 'www.example.com/index.html';
$string = file_get_contents($url);
?>

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.