by Guest » 18 Jan 2025, 22:09
Ich habe ein furchtbar geformtes HTML, dank MS Word 10 „Speichern unter HTM, HTML“. Hier ist ein Beispiel dessen, was ich zu desinfizieren versuche.
Code: Select all
Pros and Cons of a Website
(this is a sample of what it uses as line breaks. Take note of the tag).
A SAMPLE TEXT
...same pattern in div 1
...same...
Was ich von all dem brauche, ist:
Code: Select all
...A SAMPLE TEXT
...same pattern in div 1
...same...
Was ich bisher habe:
Code: Select all
$dom = new DOMDocument;
$dom->loadHTML($filecontent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$body = $xpath->query('//html/body');
$nodes = $body->item(0)->getElementsByTagName('*');
foreach ($nodes as $node) {
if($node->tagName=='script') $node->parentNode->removeChild($node);
if($node->tagName=='a') continue;
$attrs = $xpath->query('@*', $node);
foreach($attrs as $attr) {
$attr->parentNode->removeAttribute($attr->nodeName);
}
}
echo str_ireplace(['', ''], '', $dom->saveHTML($body->item(0)));
Es gibt mir:
Code: Select all
Pros and Cons of a Website
A SAMPLE TEXT
...same pattern in div 1
...same...
Womit ich gut zurechtkomme, aber ich möchte das Body-Tag entfernen. Ich möchte auch h1 und den Inhalt haben, aber wenn ich sage:
Code: Select all
if($node->tagName=='script' || $node->tagName=='h1') $node->parentNode->removeChild($node);
Etwas Seltsames passiert:
Ich bin auf einige sehr gute Antworten gestoßen, wie zum Beispiel:
- Wie bekomme ich innerHTML von DOMNode? (Haim Evgis Antwort, ich weiß nicht, wie ich sie richtig umsetzen soll, Keyacoms Antwort auch), Marco Marsalas Antwort kommt mir am nächsten, aber die Divs haben alle ihre Klassen behalten.
Ich habe ein furchtbar geformtes HTML, dank MS Word 10 „Speichern unter HTM, HTML“. Hier ist ein Beispiel dessen, was ich zu desinfizieren versuche.
[code]
Pros and Cons of a Website
(this is a sample of what it uses as line breaks. Take note of the tag).
A SAMPLE TEXT
...same pattern in div 1
...same...
[/code]
Was ich von all dem brauche, ist:
[code]...A SAMPLE TEXT
...same pattern in div 1
...same...
[/code]
Was ich bisher habe:
[code]$dom = new DOMDocument;
$dom->loadHTML($filecontent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$body = $xpath->query('//html/body');
$nodes = $body->item(0)->getElementsByTagName('*');
foreach ($nodes as $node) {
if($node->tagName=='script') $node->parentNode->removeChild($node);
if($node->tagName=='a') continue;
$attrs = $xpath->query('@*', $node);
foreach($attrs as $attr) {
$attr->parentNode->removeAttribute($attr->nodeName);
}
}
echo str_ireplace(['', ''], '', $dom->saveHTML($body->item(0)));
[/code]
Es gibt mir:
[code]
Pros and Cons of a Website
A SAMPLE TEXT
...same pattern in div 1
...same...
[/code]
Womit ich gut zurechtkomme, aber ich möchte das Body-Tag entfernen. Ich möchte auch h1 und den Inhalt haben, aber wenn ich sage:
[code]if($node->tagName=='script' || $node->tagName=='h1') $node->parentNode->removeChild($node);
[/code]
Etwas Seltsames passiert:
[code]
becomes
[/code]
Ich bin auf einige sehr gute Antworten gestoßen, wie zum Beispiel:
[list]
[*]Wie bekomme ich innerHTML von DOMNode? (Haim Evgis Antwort, ich weiß nicht, wie ich sie richtig umsetzen soll, Keyacoms Antwort auch), Marco Marsalas Antwort kommt mir am nächsten, aber die Divs haben alle ihre Klassen behalten.
[/list]