]*(?:id="([^"]*)")?[^>]*>(.*?)<\/h\1>/is', $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { $level = (int) $m[1]; $id = !empty($m[2]) ? $m[2] : ''; $text = wp_strip_all_tags($m[3]); if ($text === '') continue; if ($id === '') { $id = 'toc-' . sanitize_title($text); } $headings[] = ['level' => $level, 'id' => $id, 'text' => $text]; } } if (empty($headings)) return; ?>