Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:23:46+00:00 2026-06-04T02:23:46+00:00

I am parsing xml file from url(in code below), using file_get_contents() function, and simpleXML,

  • 0

I am parsing xml file from url(in code below), using file_get_contents() function, and simpleXML, to insert data into the table, i did well, but i have problem with encoding(russian words) i get this ->Черногория; file and database encoding is set to utf-8;

require_once 'mysql_connect.php';
/**
 *
 *
 */
error_reporting(E_ALL);
$sql = "CREATE TABLE IF NOT EXISTS `db_countries` (
   `id` int(11) unsigned NOT NULL auto_increment,
   `countrykey` varchar(255) NOT NULL default '',
   `countryname` varchar(255) NOT NULL default '',
   `countrynamelat` varchar(500) NOT NULL default '',
   PRIMARY KEY  (`id`)
   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

mysql_query($sql);

$data = file_get_contents("http://www2.turtess-online.com.ua/export/dictionary/countries/");
$xml  = new SimpleXMLElement($data);

echo $xml->body->dictionary->element["countryName"];

foreach ($xml->body->dictionary->element as $element) {
    $countryname    = mysql_real_escape_string($element["countryName"]);
    $countrynamelat = mysql_real_escape_string($element["countryNameLat"]);
    $countrykey     = $element["countryKey"];

    if ($countrykey) {
        $q      = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . $countryname . '", "' . $countrynamelat . '")';
        mysql_query($q);
    } else {
        echo "not valid key of country";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:23:48+00:00Added an answer on June 4, 2026 at 2:23 am

    Make sure you insert Unicode content as well, database charset is not doing any “automagic” conversion.

    As an alternative, I would suggest utf8_encode($countryname) as in :

    if ($countrykey) {
        $q      = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . cp1251_to_utf8($countryname) . '", "' . $countrynamelat . '")';
        mysql_query($q);
    } else {
        echo "not valid key of country";
    }
    

    update : indeed, the XML source file shows a Windows 1251 charset

    UPDATE(2) : i tested the code against this nifty little function and it works at last 🙂

    function cp1251_to_utf8($s) 
       { 
       if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "WINDOWS-1251") 
         { 
         $c209 = chr(209); $c208 = chr(208); $c129 = chr(129); 
         for($i=0; $i<strlen($s); $i++) 
           { 
           $c=ord($s[$i]); 
           if ($c>=192 and $c<=239) $t.=$c208.chr($c-48); 
           elseif ($c>239) $t.=$c209.chr($c-112); 
           elseif ($c==184) $t.=$c209.$c209; 
           elseif ($c==168)    $t.=$c208.$c129; 
           else $t.=$s[$i]; 
           } 
         return $t; 
         } 
       else 
         { 
         return $s; 
         } 
        }
    

    credit goes to Martin Petrov

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using feedparser for parsing from XML file.But I couldn't parse <geo:lat> ,
I have some code which fetches an XML file from a URL and then
I am using the following code by the Android-people-XMLParsing example for parsing XML file
I got a string from parsing a XML file which looks like this: Fri,
I've used following code for parsing XML file in c++. http://www.codeproject.com/Articles/176236/Parsing-an-XML-file-in-a-C-C-program . Now I
I am parsing an xml file. One of the method is below : public
I have a persistence.xml file using JDBC, working fine, but the code updates the
I have this NSXMLParser that parsing an XML file from a webserver parser =
ok so I am trying to parse XML data into a table, this is
I have 2 questions: 1-I need to parse XML file and insert the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.