I believe your first replacement code should be this:
PHP Code:
//extracts title
if ( eregi("<title *>([^<>]*)</title *>",$text,$regs) ) {
$title = trim($regs[1]);
}
else {
$title = "Untitled";
}
if (!($title)) { $title = "Untitled"; } // account for regex match