2012-04-09 48 views
1

我叫谷歌地理位置服务是这样的:字符编码与file_get_contents()函数/ PHP

 $querystring = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=".$row['lat'].",".$row['lng']; 

     $context = stream_context_create(array(
      'http' => array(
       'timeout' => 10 
      ) 
     )); 

     $f = file_get_contents($querystring, 0, $context); 
     $f = json_decode($f); 

     if(!isset($f->status) || $f->status != 'OK') continue; 

     $f = $f->results[0]; 
     $location = $f->formatted_address; 

我再插入位置到数据库:

 //add the location to the database 
     $sql = "INSERT INTO `wp_postmeta` 
      (`meta_id`, `post_id`, `meta_key`, `meta_value`) 
      VALUES 
      (
       NULL, 
       '".intval($row['ID'])."', 
       'location', 
       '".mysql_real_escape_string($location)."' 
      )"; 
     if($location) $insert = mysql_query($sql); 

问题:某些字符当我编辑WordPress的帖子时,显示效果不佳。 例如 “R.HilïrioRiberio,41-243 - 巴西里约热内卢Praçada Bandeira,20270-180,” 应该是 “R.HilárioRiberio,41-243 - Praçada Bandeira ,里约热内卢,20270-180,巴西”

我使用UTF-8作为网站上的字符编码。但这是无关紧要的,因为phpMyAdmin显示数据与破碎的字符一起输入到数据库中。

如何设置正确的字符编码?

回答

2

试试这个:

  • 首先,让我们来检查你的PHP +浏览器的正确使用UTF-8,以便调试的实际问题的工作。将此放在你的PHP文件,并测试在浏览器中:

    echo 'Filosofía: φιλοσοφία'; 
    

    你应该能够阅读它,就像这里显示(在西班牙和希腊)

  • 一旦你确定UTF-8正常工作,输出你的$f数组的内容:

    echo "<pre>\n" . print_r($f, true) . "\n</pre>"; 
    

    检查的内容看乱码。

  • 请务必告诉MySQL要使用UTF-8:

    mysql_set_charset('utf8'); 
    

    这个地方马上mysql_connect()功能。

+0

测试#1可以帮助,但它不是100%可靠。我看过太多的真实案例,在存储它之前脚本会破坏数据,并在显示它之前完全“破坏”它。 – 2012-04-09 09:36:25