How to change an entire website's files' character encoding to UTF-8?

孤者浪人 提交于 2021-01-29 07:57:58

问题


How can I change all of my php/css/js files to utf character encoding? I can't get it to validate otherwise.


回答1:


Have you tried the .htaccess option "AddCharset". It should work on the parent directory of where the .htaccess file is located plus all sub directories.

AddCharset UTF-8 .php

http://www.w3.org/International/questions/qa-htaccess-charset




回答2:


You haven't said what encoding the files are in at the moment, or what operating system you are using. On my Ubuntu system, I have a command called iconv that will convert a single file. Using this, I would write a simple script to convert files "in-place". For example, to convert from iso8859-15 to utf-8, put this script in /tmp/convert:

#!/bin/sh
iconv -f iso8859-15 -t utf-8 "$1" > "$1.$$" && mv "$1.$$" "$1"

then run:

chmod 755 /tmp/convert

and then run find to execute that script on all .php, .js and .css files:

find /my/website/root -name \*.php -o -name \*.js -o -name \*.css -exec /tmp/convert {} \;

Remember to back up your files first in case the conversion doesn't do what you expect.



来源:https://stackoverflow.com/questions/4675073/how-to-change-an-entire-websites-files-character-encoding-to-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!