Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4923

Re: DOMDocument HTML problem

From "Aaron Gray" <ang.usenet@gmail.com>
Newsgroups comp.lang.php
Subject Re: DOMDocument HTML problem
Date 2012-02-21 20:08 +0000
Message-ID <9qibu5FgjnU1@mid.individual.net> (permalink)
References <9qi7vtFfmvU1@mid.individual.net>

Show all headers | View raw


"Aaron Gray"  wrote in message news:9qi7vtFfmvU1@mid.individual.net... 

Hi, I am trying to take an incomplete "summary"  of a number of characters 
of an HTML fragment without DOCTYPE, HTML, or BODY elements and a possibly 
incomplete unbalanced fragment, and convert it to a ballanced fragment 
without DOCTYPE, HTML, or BODY, using DOMDocument and friends.

~~~
<?php

$html = '<div><div><span>
<p><a href="#test">foo</a></p>
<hr>
<br>
<span>name</span>
';

$html = trim( preg_replace( '/\s\s+/', '', $html));

echo $html . "\n\n";

$dom= new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$body = $xpath->query('/html/body/*');

echo $dom->saveXml($body->item(0));

?>
~~~

This does the job !

Aaron

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

DOMDocument HTML problem "Aaron Gray" <ang.usenet@gmail.com> - 2012-02-21 19:00 +0000
  Re: DOMDocument HTML problem Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-21 14:14 -0500
  Re: DOMDocument HTML problem "Aaron Gray" <ang.usenet@gmail.com> - 2012-02-21 20:08 +0000

csiph-web