phppaper Posted June 16, 2008 Share Posted June 16, 2008 Hello, there is a table within a webpage and I want to use php to get the text within the table, but I just want to get the text inside the table not the entire html webpage, which PHP function I can do this job?? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/ Share on other sites More sharing options...
vbnullchar Posted June 16, 2008 Share Posted June 16, 2008 hmm... i thinks its easier to use javascript to do that.. php is server side Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566324 Share on other sites More sharing options...
phppaper Posted June 16, 2008 Author Share Posted June 16, 2008 can u teach me how ?? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566327 Share on other sites More sharing options...
phppaper Posted June 16, 2008 Author Share Posted June 16, 2008 by the way the webpage is not mine, I just want to get the data from the table of that webpage to my own site Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566328 Share on other sites More sharing options...
vbnullchar Posted June 16, 2008 Share Posted June 16, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566329 Share on other sites More sharing options...
thebadbad Posted June 16, 2008 Share Posted June 16, 2008 It's not very complex to do this with PHP, using regular expressions. If you post the link to the website, and tell me specifically what you want to scrape (what it's normally called), I'll give you an example script. Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566330 Share on other sites More sharing options...
phppaper Posted June 16, 2008 Author Share Posted June 16, 2008 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!! Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566342 Share on other sites More sharing options...
vbnullchar Posted June 16, 2008 Share Posted June 16, 2008 heres how.. mootools way var tds = $$('td'); tds.each(function(td){ alert(td.getText()); }); Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566346 Share on other sites More sharing options...
vbnullchar Posted June 16, 2008 Share Posted June 16, 2008 native javascript var tds = document.getElementsByTagName('td'); for(var i=0; i<=tds.length; i++) ..... Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566350 Share on other sites More sharing options...
thebadbad Posted June 16, 2008 Share Posted June 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566351 Share on other sites More sharing options...
phppaper Posted June 16, 2008 Author Share Posted June 16, 2008 awesome!! Thanks the 2 of you, specially to thebadbad , just what I am looking for, thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/110386-use-php-to-get-some-part-of-html-from-a-website/#findComment-566355 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.