Getting data between html tags or string phrases

2007 Feb 20

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 = 'Tildemark blogs';
$title = substring_between($content,'','');

the code above returns the title of an html page.

Tweet this post

Leave a comment


Recent Entries

Close