Nasıl boş paragrafları simple_html_dom.php kullanarak bir HTML dosyasından kaldırabilirsiniz?

0 Cevap php

I simple_html_dom.php kullanarak bir HTML belgeden boş paragraflar kaldırmak istiyorum. MS Word'de hazırlanan çalışmak HTML dosyaları, DOMDocument en loadHTMLFile () fonksiyonu bu istisna "Ad tanımlı değil" verir çünkü, DOMDocument sınıfını kullanarak bunu nasıl biliyorum ama.

Bu MS Word'de hazırlanan HTML dosyaları için DOMDocument nesnesi ile kullanmak kodu:

<?php
/* Using the DOMDocument class */

/* Create a new DOMDocument object. */
$html = new DOMDocument("1.0", "UTF-8");

/* Load HTML code from an HTML file into the DOMDocument. */
$html->loadHTMLFile("HTML File With Empty Paragraphs.html");

/* Assign all the <p> elements into the $pars DOMNodeList object. */
$pars = $html->getElementsByTagName("p");

echo "The initial number of paragraphs is " . $pars->length . ".<br />";

/* The trim() function is used to remove leading and trailing spaces as well as
* newline characters. */
for ($i = 0; $i < $pars->length; $i++){
    if (trim($pars->item($i)->textContent) == ""){
        $pars->item($i)->parentNode->removeChild($pars->item($i));
        $i--;
    }
}

echo "The final number of paragraphs is " . $pars->length . ".<br />";

// Write the HTML code back into an HTML file.
$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");
?>

Bu MS Word'de hazırlanan HTML dosyaları için simple_html_dom.php modülü ile kullanmak kodu:

<?php
/* Using simple_html_dom.php */

include("simple_html_dom.php");

$html = file_get_html("HTML File With Empty Paragraphs.html");

$pars = $html->find("p");

for ($i = 0; $i < count($pars); $i++) {
    if (trim($pars[$i]->plaintext) == "") {
        unset($pars[$i]);
        $i--;
    }
}

$html->save("HTML File without Empty Paragraphs.html");
?>

Bu $ simple_html_dom.php kullanırken DOMDocument ve bir dizi kullanırken değişken bir DOMNodeList olduğunu pars o dışında, hemen hemen aynıdır. Ama bu kod çalışmaz. İlk iki dakika boyunca çalışır ve daha sonra bu hataları bildirir: "Undefined offset: 1" ve bu hat için "nonobject mülkiyet elde etmek için çalışmak": "(($ pars [$ i] Döşeme - eğer> şifresiz) ==" " ) {".

Herkes bunu düzeltmek nasıl biliyor mu?

Teşekkür ederim.

Ben de sordu php devnetwork.

0 Cevap