« Top Searched Keywords | Main | Enable htaccess on Apache »

Getting data between html tags or string phrases

Home » Programming » PHP

Here's a function from the php manual that returns everything inside two tags or between two string phrases. As we all know, substr requires the integer position of the string to return, so it would be hard if we are only given with start string and end string but not its integer position.


function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);
}
}
$content = '<title>Tildemark blogs</title>';
$title = substring_between($content,'<title>','</title>');

the code above returns the title of an html page.

TrackBack

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

About

This page contains a single entry from the blog posted on February 20, 2007 9:58 AM .

The previous post in this blog was Top Searched Keywords .

The next post in this blog is Enable htaccess on Apache .

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