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

1 Comment

the problem with this code is if you have multipe versioons of the tag and want the data inside all of them you cant get it..

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