christa Posted May 13, 2010 Share Posted May 13, 2010 hi all, how to grab only some data from an external web site? Example: from http://www.phpfreaks.com/forums i want to obtain only some text as Home Forums Chat Help Search Profile My Messages Members Rules & TOS Logout and print it in my web page. How to do this operation? thanks! Quote Link to comment Share on other sites More sharing options...
Kryptix Posted May 13, 2010 Share Posted May 13, 2010 You could use a function like file_get_contents() to put all the HTML into a string, such as: <?php $html = file_get_contents("http://www.phpfreaks.com"); ?> You can then split every line into an array by using explode() and loop through them looking for specific text: <?php $html = explode("\n", file_get_contents("http://www.phpfreaks.com")); foreach ($html as $data) { if (preg_match("/TEXT HERE/", $data)) echo $data; } ?> The pattern you use depends on the website you're grabbing data from. If you post the website you need to get text from and the exact text you need to grab I'll have a look. Quote Link to comment Share on other sites More sharing options...
christa Posted May 13, 2010 Author Share Posted May 13, 2010 the web site is: http://www.n2yo.com/?s=24905 and the data to grab are in the right box (under YOUR CURRENT SELECTION): Latitude: Longitude:.... until Eclipsed? How to do? Quote Link to comment Share on other sites More sharing options...
Kryptix Posted May 13, 2010 Share Posted May 13, 2010 Well, the following code works but the problem is, the text on the original site updates constantly using Ajax, so when the page is first loaded most of the text isn't there and the text that is there hasn't been updated to the correct information yet... I'm not sure how to grab text from another website that updates with Ajax to be honest. <?php $html = explode("\n", file_get_contents("http://www.n2yo.com/?s=24905")); $lines = array("Latitude:", "Longitude:"); foreach ($html as $data) { foreach ($lines as $string) { if (preg_match("\"" . $string . "\"", $data)) echo str_replace( " <td><font size=2>", "", str_replace( "</td><td><font size=2><B>", "", str_replace( "</B></td>", "", $data ) ) ) . "<br />"; } } ?> If you can find another source I can possibly help. Quote Link to comment 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.