<?php
preg_match_all
('/\<\/.*?[> ]/'$ofx_sgml$tags);
$tags $tags[0];
$closing_tags array_fill_keys($tagsfalse);
$lines explode("\n"$ofx_sgml);
$ofx_xml '';
foreach(
$lines as $line)
    {
    
$ofx_xml .= $line;
    
preg_match_all('/\<[^\/].*?\>/'$line$tags);
    
$tags $tags[0];
    if(
count($tags) > 0)
        {
        
$tags array_reverse($tags);
        foreach(
$tags as $tag)
            {
            
$closing_tag str_replace('<''</'$tag);
            if(!
array_key_exists($closing_tag$closing_tags))
                {
                
$ofx_xml .= $closing_tag;
                }
            }
        }
    
$ofx_xml .= "\n";
    }
header("Content-type: text/xml");
die(
$ofx_xml);
?>