Canman2005 Posted January 6, 2009 Share Posted January 6, 2009 Is it possible to supply a URL of an external site in PHP and grab whatever is held between two defined tags. For example, if you wanted to grab news from a website, then providing the URL and HTML tags the news you want to grab in contained within and then PHP would pull through each result on the page? Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/ Share on other sites More sharing options...
DarkWater Posted January 6, 2009 Share Posted January 6, 2009 It's absolutely possible. I'd suggest checking out the cURL library and regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730505 Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 Yes cURL is very popular but depending on you situation you may want to use file_get_contents() instead. Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730508 Share on other sites More sharing options...
DarkWater Posted January 6, 2009 Share Posted January 6, 2009 That works too, but cURL is faster. And if you need to POST any data or log in or use cookies for the site, you need cURL. file_get_contents() is certainly easier though. Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730509 Share on other sites More sharing options...
Canman2005 Posted January 6, 2009 Author Share Posted January 6, 2009 So how do you define what tags to grab between on file_get_contents()? I have had a look around and can see how to grab so many bytes of a page or a full page but not defining what tags Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730543 Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 You need to see what the format of the page is and use Regex. There are many examples on phpfreaks and millions on the web. Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730547 Share on other sites More sharing options...
xtopolis Posted January 6, 2009 Share Posted January 6, 2009 DarkWater wrote a tutorial on reg ex with PHP. It even shows a grabbing-between-tags example. Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730549 Share on other sites More sharing options...
RussellReal Posted January 6, 2009 Share Posted January 6, 2009 regex is slow use strpos with substr example: <?php $d = file_get_contents("http://theWebsite.com/file.php"); $tag = "<h1>"; $s = strpos($d,$tag) + strlen($tag); $e = strpos($d,"</".substr($tag,1),$s); $content = substr($d,$s,$e - $s); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139625-grabbing-content-from-url/#findComment-730634 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.