Getting specific tag contents

2006 Oct 1

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.

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 = '

';
$end_tag = '

';
$url = 'https://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.

Tweet this post

1 Comment

Hi,
small sytax change in the code. One is $match[1] on return. And parameter was missing. when supplied.

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 = '

';
$end_tag = '

';
$url = 'https://tildemark.com/';
$tag_contents = get_tag_contents($start_tag, $end_tag,$url);
print $tag_contents;

Leave a comment


About Me


Alfredo Sanchez is an internet professional focusing on the study search engines behavior in particular. Supports Free Open Source Software and currently develops applications with it using XAMPP.

Recent Entries

Close