简体   繁体   中英

utf-8 encoding in DOMDocument class

I am using PHP's DOMDocument class to open an HTML file, make some changes, and save it. The problem is that the javascript in every page that is loaded, contains utf 8 characters, and when I save the HTML file, these characters are "changed". Here's my code.

$doc=new DOMDocument();
$doc->loadHTMLFile($path);

$doc->encoding='UTF-8';
$doc->saveHTMLFile($path);

The file is saved again but the special characters gets distorted. eg if my javascript had an image path set to french/Comment ça s'écrit/ , this becomes french/Comment ça s’écrit  . The wierd thing is that this only happens to my javascript paths, the html elements dont get changed and are in the correct encoding.

try placing your javascript between CDATA tags as shown below

<script type="text/javascript">
/*<![CDATA[*/
    // your javascript here
/*]]>*/
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM