?
Solved

Changing character sets for ALL files in a directory to UTF-8

Posted on 2007-08-01
3
Medium Priority
?
221 Views
Last Modified: 2010-05-18
Hello,

I have over 300 script files in a directory that were created using a mixture of the following character sets:

Windows-1251
Windows-1252
ISO 8859-1

This causes a number of problems.

For example, when displayed as a webpage, the header could be created with one charset, the body with another and the footer with another.

So I need to convert the contents of ALL files with the extension .pl or .cgi or .php or .txt or .tpl to UTF-8.

How can this be done with PERL?

Thanks!
0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
mjcoyne earned 1000 total points
ID: 19613086
Are these CGI files?  Do you mean to say that the Perl scripts create web pages that use XML document type declarations (DTD) and that these declarations specify different character types?

So, some web pages created by these scripts have something like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

while others might have:

<?xml version="1.0" encoding="ISO 8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

and you need them all to be consistant?

I guess I might be confused about what you mean by the "script files...were created using a mixture of...character sets".  Do you mean the files *themselves* were created (written) with mixed character sets, or the files that the scripts create as *output* contain mixed character sets?
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 19629387
If it's what I think, it'd be a simple regular expression problem.  For example:

#!/usr/bin/perl -w
use strict;

while (<DATA>) {
    s/encoding=".+?"/encoding="UTF-8"/;
    print;
}

__DATA__
<?xml version="1.0" encoding="Windows-1251"?>
<?xml version="1.0" encoding="Windows-1252"?>
<?xml version="1.0" encoding="ISO 8859-1"?>

Couple this with File::Find to find all your .pl , .cgi, .php, .txt or .tpl files, open each matching file, replace the encoding line with the one you want, and print the file to a new file.
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 1000 total points
ID: 19639207
# I guess you need something like following code snippet:

use Encode;
# ... some code to read the file content here
from_to($content, "cp1250", "utf8");
# .. some other file
from_to($content, "iso-8859-1", "utf8");

# please read  man Encode carefully first
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question