mcastles Posted February 24, 2007 Share Posted February 24, 2007 Hi, I've been using DomDocument in PHP5 and its been working really well. I just have a few questions about it: I'm loading a html file using: $dom = new DomDocument(); @$dom->loadHTMLFile("html.html"); then I'm modifying the domdocument and then save the html using: echo $dom->saveHTML(); This all works fine and I'm fairly happy with it. The only issue I have is that the new output generates invalide html code. Basically link tags and meta tags don't have closing tags anymore. For example.. The original html file might have some link tags like this: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <link rel="stylesheet" type="text/css" href="default.css" media="screen" /> <link rel="stylesheet" type="text/css" href="print.css" media="print" /> but after loading and saving with DomDocument it outputs like this: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <link rel="stylesheet" type="text/css" href="default.css" media="screen"> <link rel="stylesheet" type="text/css" href="print.css" media="print"> Has anyone come accross this and know a workaround? Oh, and one other thing: does $dom->formatOutput = true; and $dom->preserveWhiteSpace = false; do anything for loaded html files? I can't seem to get it to modify the output at all. Quote Link to comment Share on other sites More sharing options...
mcastles Posted February 24, 2007 Author Share Posted February 24, 2007 For anyone interested... I have come up with a workaround to convert link, meta and break return tags to have closing slashes. $search = array('/<link(.*)>/i','/<meta(.*)>/i','/<br(.*)>/i'); $replace = array('<link$1/>','<meta$1/>','<br$1/>'); $html = preg_replace($search,$replace, $html); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.