private function getGedcomValue($tag, $level, $gedrec): string
    {
        if (empty($gedrec)) {
            return '';
        }
        $tags      = explode(':', $tag);
        $origlevel = $level;
        if ($level == 0) {
            $level = $gedrec[0] + 1;
        }

        $subrec = $gedrec;
        foreach ($tags as $t) {
            $lastsubrec = $subrec;
            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
            if (empty($subrec) && $origlevel == 0) {
                $level--;
                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
            }
            if (empty($subrec)) {
                if ($t === 'TITL') {
                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
                    if (!empty($subrec)) {
                        $t = 'ABBR';
                    }
                }
                if (empty($subrec)) {
                    if ($level > 0) {
                        $level--;
                    }
                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
                    if (empty($subrec)) {
                        return '';
                    }
                }
            }
            $level++;
        }
        $level--;
        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
        if ($ct == 0) {
            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
        }
        if ($ct == 0) {
            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
        }
        if ($ct > 0) {
            $value = trim($match[1]);
            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
                $note = Note::getInstance($match[1], $this->tree);
                if ($note instanceof Note) {
                    $value = $note->getNote();
                } else {
                    //-- set the value to the id without the @
                    $value = $match[1];
                }
            }
            if ($level != 0 || $t != 'NOTE') {
                $value .= Functions::getCont($level + 1, $subrec);
            }

            return $value;
        }

        return '';
    }