开发者

Encoding Conversion with PHP

Trying to do a Latin1 to UTF-8 conversion for WordPress, had no luck with the tutorial posted in the Codex. I came up with this to check encoding and convert.

while($row = mysql_fetch_assoc($sql)) {
if(!mb_check_encoding($row['post_content'], 'UTF-8')) {

    $row = mb_convert_encoding($row['post_content'], 'ISO-8859-1', 'UTF-8');

    if(!mb_check_encoding($row['post_content'], 'UTF-8')) {
        echo 'Can\'t Be Converted<br/>';    
    }
    else {
        echo '<br/>'.$row.'<br/><br/>';开发者_StackOverflow中文版
    }
}
else {
    echo 'UTF-8<br/>';
}
}

This works... sorta. I'm not getting any rows that can't converted but I did notice that Panamá becomes Panam

Am I missing a step? Or am I doing this all wrong?

UPDATE

The data stored within the database is corrupt(á characters are stored). So its looking more like a find and replace job than a conversion. I haven't found any great solutions so far for doing this automagically.


This will help you. http://php.net/manual/en/book.iconv.php

Further more you can set your mysql connection to utf8 this way:

mysql_set_charset  ('utf8',$this->getConnection());  

$this->getConnection in my code returns the variable which was returned by

mysql_connect(MYSQL_SERVER,DB_LOGIN,DB_PASS);


Refer to the PHP documentation for mb_convert_encoding:

string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )

Your code is attempting to convert to ISO-8859-1 from UTF-8!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜