Jump to content

use php to get some part of html from a website


phppaper

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.