Discharge Summary (possibly followed by
Discharge Summary
) // when there is no .discharge-summary-content immediately after it $pattern = '/]*class="[^"]*discharge-section-heading[^"]*"[^>]*>\s*Discharge\s+Summary\s*<\/h[1-6]>\s*(?:
\s*\s*Discharge\s+Summary\s*<\/strong>\s*<\/div>\s*)?(?!
)/i'; $html = preg_replace($pattern, '', $html); // 2. Also remove any standalone
Discharge Summary
if it repeats standard title $html = preg_replace('/
\s*\s*Discharge\s+Summary\s*<\/strong>\s*<\/div>/i', '', $html); return trim($html); } // Case 1: Template 1 with Empty summary inside {{CONTENT}} $case1 = '

Discharge Summary

Patient info

Discharge Summary

Discharge Summary

Presenting Complaints :
Abdominal Pain

'; echo "=== Case 1: Template 1 with Empty summary inside {{CONTENT}} ===\n"; echo cleanHtml($case1) . "\n\n"; // Case 2: Template with Non-empty summary with doctor notes $case2 = '

Discharge Summary

Patient info

Discharge Summary

Patient admitted with fever and responded well to IV antibiotics.

Presenting Complaints :
Abdominal Pain

'; echo "=== Case 2: Non-empty summary ===\n"; echo cleanHtml($case2) . "\n";