1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to convert full country name to 2 letter format (ISO 3166-1)

Discussion in 'PHP & Perl' started by hellnation, Jun 18, 2013.

  1. hellnation

    hellnation Regular Member

    Joined:
    Feb 15, 2013
    Messages:
    278
    Likes Received:
    62
    Occupation:
    Senior Network and System Engineer
    Location:
    Quebec, Canada
    Hi,

    I need a script to convert the full country names in a file to the 2 letter format.

    I looked around and didn't find anything, looks like the best way is to make a php script that reads a conversion table and then convert the data.

    However, I couldn't find any examples, how can I do this?

    Here is a library that can be used to do this:

    https://github.com/johannesl/Internationalization

    I'm guessing a lot of others have faced the same issue before, seems like a common problem.

    Thanks!
     
  2. hellnation

    hellnation Regular Member

    Joined:
    Feb 15, 2013
    Messages:
    278
    Likes Received:
    62
    Occupation:
    Senior Network and System Engineer
    Location:
    Quebec, Canada
    or anyone know the best place to ask for this?
     
  3. garthor

    garthor Newbie

    Joined:
    Mar 24, 2013
    Messages:
    48
    Likes Received:
    13
    I believe it's pretty much straightforward. It even comes with a PHP example :


    https://github.com/johannesl/Internationalization/blob/master/example.php


    All You need to do is simply download these two files :


    https://github.com/johannesl/Internationalization/blob/master/countryaliases.php
    https://github.com/johannesl/Internationalization/blob/master/countrynames.php


    Include them both at the top of the top of the php script you will use this code on.
    Then, you will be able to use a function called : string2countrycode.


    Code:
    $code = string2countrycode('Sweden');
    

    Basically, You pass the country name with the function and it will return the two letter format back as you'd like.
    It will be store in the code variable in this case. If you echo it, the output would be : "SE" as that's Sweden 2 letter format.
     
  4. sshaw

    sshaw Newbie

    Joined:
    Jun 19, 2013
    Messages:
    2
    Likes Received:
    0
    Hi, I have written a Ruby library and utility program that will convert to/from a variety of country names and codes. The program will convert data from a database, XML file or directory of XMLs, and, at some point, CSV.

    It can be found at (note that this forum wont allow me to post links, so I had to improvise): it's on GitHub at /sshaw/normalize_country

    You'll have to download it from github as the version on rubygems (public repository of Ruby libraries) does not include the utility program.
    Also note that I haven't finished the CSV option.

    I only used the library so I'd appreciate any feedback on the command line program.

    Be sure to backup your data before using this. It is a work in progress.
     
  5. sshaw

    sshaw Newbie

    Joined:
    Jun 19, 2013
    Messages:
    2
    Likes Received:
    0
    Hellnation, I can't send private messages until I have "15 posts". I will take a look at the permission issue. It might be a problem with the script as I have never ran it on Windows.

    What's the format of your XML file?