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

Posted on 2007-08-01
Last Modified: 2010-05-18

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

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?

Question by:hankknight
    LVL 17

    Accepted Solution

    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" "">

    while others might have:

    <?xml version="1.0" encoding="ISO 8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

    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?
    LVL 17

    Expert Comment

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

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

    while (<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.
    LVL 51

    Assisted Solution

    # 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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    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…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now