my code stock.com

Obtain data from url and force links to work
This code was written by Michael Devaney
Note: You might want to cache this

Snippet options

Download: Download snippet as advance-curl-scrape.php.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

cr<?php
//  Advanced cURL Scrape
//  Obtain data from url and force links to work
//  This code was written by Michael Devaney
//  Note: You might want to cache this
//  All I ask is that you do not remove these notes
//  Thank you
$ch = curl_init ("http://www.theurl.com"); //URL to Scrape
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

preg_match('#</p[^>]*>(.+?)</p>#is', $page, $matches, $links); // grabs anything between <p> </p> tags
foreach ($matches as &$match) {
    $match = str_replace('href="', 'href="http://www.theurl.com', $match); 
	//This is used to make sure links work.
}
// output html, styles, and more.
echo '<head>';
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'; // make sure its able to be read correctlly
echo '</head>';
echo '<style type="text/css"></style>'; //add styles here
echo '<body>';
echo '<img src="http://www.yoursite.com/image.jpg">'; //Maybe add an image over the scaped data
echo '<h1>cURL Scrape</h1>';
echo $matches[1]; // change [1] to [2] If you want to grab data between the second <p></p> tags  
echo '</body>';
?>

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.