Yep, preg_match matches on the first h2 tag. Try the following instead:
Code:
//extracts title
if (preg_match_all('/< *h2 *>(.*?)< *\/ *h2 *>/is',$text,$regs,PREG_SET_ORDER)) {
$title = trim($regs[X][1]); // set X to a number
}
else {
$title = "";
}
Where X in the above code indicates the h2 tag to match: $regs[0][1] means to match the first h2 tag, $regs[1][1] means to match the second h2 tag, $regs[2][1] means to match the third h2 tag, and so on.
You could also do something like the following:
Code:
// assumes there are at least three h2 tags
$title = trim($regs[0][1]." ".$regs[1][1]." ".$regs[2][1]);
A thing to remember is that whatever you set X to, that will be used across all pages, so there needs to be at least X+1 h2 tags in all pages.