PHP DOMDocument loadHTML not encoding UTF-8 correctly

后端 未结 13 1526
梦如初夏
梦如初夏 2020-11-22 15:11

I\'m trying to parse some HTML using DOMDocument, but when I do, I suddenly lose my encoding (at least that is how it appears to me).

$profile = \"

        
相关标签:
13条回答
  • 2020-11-22 15:34

    This worked for me.

    In php.ini file, change the following property.

    Before:

    mbstring.encoding_transration = On
    

    After:

    mbstring.encoding_transration = Off
    
    0 讨论(0)
  • 2020-11-22 15:35

    Make sure the real source file is saved as UTF-8 (You may even want to try the non-recommended BOM Chars with UTF-8 to make sure).

    Also in case of HTML, make sure you have declared the correct encoding using meta tags:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    

    If it's a CMS (as you've tagged your question with Joomla) you may need to configure appropriate settings for the encoding.

    0 讨论(0)
  • 2020-11-22 15:38

    The problem is with saveHTML() and saveXML(), both of them do not work correctly in Unix. They do not save UTF-8 characters correctly when used in Unix, but they work in Windows.

    The workaround is very simple:

    If you try the default, you will get the error you described

    $str = $dom->saveHTML(); // saves incorrectly
    

    All you have to do is save as follows:

    $str = $dom->saveHTML($dom->documentElement); // saves correctly
    

    This line of code will get your UTF-8 characters to be saved correctly. Use the same workaround if you are using saveXML().


    Update

    As suggested by "Jack M" in the comments section below, and verified by "Pamela" and "Marco Aurélio Deleu", the following variation might work in your case:

    $str = utf8_decode($dom->saveHTML($dom->documentElement));
    

    Note

    1. English characters do not cause any problem when you use saveHTML() without parameters (because English characters are saved as single byte characters in UTF-8)

    2. The problem happens when you have multi-byte characters (such as Chinese, Russian, Arabic, Hebrew, ...etc.)

    I recommend reading this article: http://coding.smashingmagazine.com/2012/06/06/all-about-unicode-utf8-character-sets/. You will understand how UTF-8 works and why you have this problem. It will take you about 30 minutes, but it is time well spent.

    0 讨论(0)
  • 2020-11-22 15:42

    You must feed the DOMDocument a version of your HTML with a header that make sense. Just like HTML5.

    $profile ='<?xml version="1.0" encoding="'.$_encoding.'"?>'. $html;
    

    maybe is a good idea to keep your html as valid as you can, so you don't get into issues when you'll start query... around :-) and stay away from htmlentities!!!! That's an an necessary back and forth wasting resources. keep your code insane!!!!

    0 讨论(0)
  • 2020-11-22 15:43

    The problem is that when you add a parameter to DOMDocument::saveHTML() function, you lose the encoding. In a few cases, you'll need to avoid the use of the parameter and use old string function to find what your are looking for.

    I think the previous answer works for you, but since this workaround didn't work for me, I'm adding that answer to help people who may be in my case.

    0 讨论(0)
  • 2020-11-22 15:45

    This took me a while to figure out but here's my answer.

    Before using DomDocument I would use file_get_contents to retrieve urls and then process them with string functions. Perhaps not the best way but quick. After being convinced Dom was just as quick I first tried the following:

    $dom = new DomDocument('1.0', 'UTF-8');
    if ($dom->loadHTMLFile($url) == false) { // read the url
        // error message
    }
    else {
        // process
    }
    

    This failed spectacularly in preserving UTF-8 encoding despite the proper meta tags, php settings and all the rest of the remedies offered here and elsewhere. Here's what works:

    $dom = new DomDocument('1.0', 'UTF-8');
    $str = file_get_contents($url);
    if ($dom->loadHTML(mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8')) == false) {
    }
    

    etc. Now everything's right with the world. Hope this helps.

    0 讨论(0)
提交回复
热议问题