PDA

View Full Version : Display first H2 tag as title instead of page title


mdavila
02-25-2005, 01:19 PM
We have thousands of pages in our site and most of the pages within a section share the same page title, so the search results can look confusing since all the titles on the results page have the same text. Is it possible to get phpdig to display the text in between the h2 tags on the page? since that is a better reflection of the title of the page?

Thanks,
-Marc

Charter
02-26-2005, 08:37 PM
Look for the phpdigCleanHtml function in the robot_functions.php file, and in the if-else "//extract title" code snippet, try replacing title with h2 and see if that works.

mdavila
02-27-2005, 06:04 PM
Such as this?

//extracts title
if (preg_match('/< *h2 *>(.*?)< *\/ *h2 *>/is',$text,$regs)) {
$title = trim($regs[1]);
}
else {
$title = "";
}

Charter
02-27-2005, 06:29 PM
Yes, try that and see if it sets the title to be the content between the h2 tags. BTW, how many h2 tags are there per page? The preg_match will look for the first h2 to match.

mdavila
02-27-2005, 06:30 PM
there are more than 1. I tried it but it picks up the first one, so it works but shows the content for the wrong H2. Is it possible to choose which H2 it picks up?

Charter
02-27-2005, 06:48 PM
Yep, preg_match matches on the first h2 tag. Try the following instead:

//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:

// 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.