1

I save innerHTML of an element by

function innerHTML(DOMNode $el){
    $html='';
        foreach($el->childNodes as $node){
            $temp=new DOMDocument();
            $temp->appendChild($temp->importNode($node,true));
            $html.=trim($t->saveHTML());
         }
    return (string)$html;
}

The problem is that it converts characters to HTML entities (–, α, etc).

The solution given here is to set the encoding when loading into the document as

$dom->loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES', 'UTF-8'));

but I do not load an HTML. The DOMDocument is just a temporary document for saving the imported nodes.

Googlebot
  • 15,159
  • 44
  • 133
  • 229
  • Does this answer your question? [PHP DOMDocument loadHTML not encoding UTF-8 correctly](https://stackoverflow.com/questions/8218230/php-domdocument-loadhtml-not-encoding-utf-8-correctly) – Michel Oct 02 '21 at 11:27
  • @Michel this is one of the materials I read. How can I apply `$dom->loadHTML('' . $profile);`? I don't have a document to load. Instead, I create a temporary document and importNode. How do you apply encoding when creating DOMDocument or importNode. This is indeed my question. – Googlebot Oct 02 '21 at 11:39

1 Answers1

1

You can use the PHP function html_entity_decode($html) to convert HTML entities.

DannyXCII
  • 888
  • 4
  • 12
  • it's surely a solution. However, I was familiar with the standard function for decoding HTML entities. I look for a standard method to save the original character encoding in `DOMDocument`. – Googlebot Oct 02 '21 at 09:40