Getting specific tag contents

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/&#8217;;
$tag_contents = get_tag_contents($start_tag, $end_tag, $url);
print $tag_contents;
?>
Fell free to modify this code and don’t forget to post your changes here.

Advertisements
Posted in: PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s