Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #4921 > unrolled thread
| Started by | "Aaron Gray" <ang.usenet@gmail.com> |
|---|---|
| First post | 2012-02-21 19:00 +0000 |
| Last post | 2012-02-21 20:08 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.php
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
| From | "Aaron Gray" <ang.usenet@gmail.com> |
|---|---|
| Date | 2012-02-21 19:00 +0000 |
| Subject | DOMDocument HTML problem |
| Message-ID | <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.
This is what I have got (it works without a Error 500 using php command line
tool :-
~~~~
<?php
$html = '<div>
<p><a href="#test">foo</a></p>
<hr>
<br>
<div>name</div>
';
$dom = new DOMDocument();
$newdom = new DOMDocument();
$dom->loadHTML($html);
$Elements = $dom->childNodes;
foreach ( $Elements as $Element ) {
$NewElement = $newdom->createElement($Element->nodeName);
if ($Element->attributes)
foreach($Element->attributes as $attribute)
$NewElement->setAttribute($attribute->name, $attribute->value);
if ($Element->childNodes)
foreach($Element->childNodes as $child)
$NewElement->appendChild( $newdom->importNode($child, true));
$newdom->appendChild( $NewElement);
}
echo $newdom->saveHTML();
?>
~~~~
It balances the unbalanced <div> seems to be adding a duplicate
<HTML></HTML> at the beginning of the output.
Also I need to get the fragment of the code like a JavaScript DOM innerHTML
without the <HTML> and <BODY> tags.
Many thanks in advance.
Hope you can help,
Aaron
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2012-02-21 14:14 -0500 |
| Message-ID | <ji0qf2$veh$1@dont-email.me> |
| In reply to | #4921 |
On 2/21/2012 2:00 PM, Aaron Gray wrote:
> 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.
>
> This is what I have got (it works without a Error 500 using php command
> line tool :-
>
> ~~~~
> <?php
>
> $html = '<div>
> <p><a href="#test">foo</a></p>
> <hr>
> <br>
> <div>name</div>
> ';
>
> $dom = new DOMDocument();
> $newdom = new DOMDocument();
>
> $dom->loadHTML($html);
>
> $Elements = $dom->childNodes;
>
> foreach ( $Elements as $Element ) {
>
> $NewElement = $newdom->createElement($Element->nodeName);
>
> if ($Element->attributes)
> foreach($Element->attributes as $attribute)
> $NewElement->setAttribute($attribute->name, $attribute->value);
>
> if ($Element->childNodes)
> foreach($Element->childNodes as $child)
> $NewElement->appendChild( $newdom->importNode($child, true));
>
> $newdom->appendChild( $NewElement);
> }
>
> echo $newdom->saveHTML();
> ?>
> ~~~~
>
> It balances the unbalanced <div> seems to be adding a duplicate
> <HTML></HTML> at the beginning of the output.
>
> Also I need to get the fragment of the code like a JavaScript DOM
> innerHTML without the <HTML> and <BODY> tags.
>
> Many thanks in advance.
>
> Hope you can help,
>
> Aaron
>
>
>
If your document is not well formed, DOMDocument has to guess at what it
the document is supposed to represent. Results are likely to be rather
indeterminate.
This doesn't mean you need DOCTYPE, <head>, <body>, etc., but things
like unbalanced <div> tags are likely to cause problems.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | "Aaron Gray" <ang.usenet@gmail.com> |
|---|---|
| Date | 2012-02-21 20:08 +0000 |
| Message-ID | <9qibu5FgjnU1@mid.individual.net> |
| In reply to | #4921 |
"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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web