« Google hints | Main | Scrape web contents faster »

Getting specific tag contents

Home » Programming » PHP

Here's a handy function to get specific tag contents. You could modify the tag you wish to scrape by changing the $start_tag and $end_tag variables. Useful in getting data from multiple html tags.

<?php
function get_tag_contents($start_tag, $end_tag, $url){
$data = file_get_contents($url);
preg_match( "|$start_tag(.*)$end_tag|s", $data, $match);
return match[1];
}
$start_tag = '<p>';
$end_tag = '</p>';
$url = 'http://www.tildemark.com/';
$tag_contents = get_tag_contents($start_tag, $end_tag);
print $tag_contents;
?>


Fell free to modify this code and don't forget to post your changes here.

TrackBack

TrackBack URL for this entry:
http://www.tildemark.com/cgi-bin/mt/mt-tb.cgi/32

About

This page contains a single entry from the blog posted on October 1, 2006 1:56 PM .

The previous post in this blog was Google hints .

The next post in this blog is Scrape web contents faster .

Many more can be found on the main index page or by looking through the archives .

Creative Commons License
This weblog is licensed under a Creative Commons License .
Powered by
Movable Type 3.33