You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress字符集乱码求助:主机迁移后特殊字符显示异常

Fixing Post-Migration Special Character Garbage (Geëet → Geëet, Rosè → Rosè)

Hey Werner, let’s tackle this charset headache—server migrations love messing with encoding settings, so we’ll get your special characters back without breaking the site again.

First, let’s undo that crash-inducing change:

  • Restore the commented-out lines immediately:
    define('DB_CHARSET', 'utf8');
    define('DB_COLLATE', '');
    
    Commenting these out breaks the database connection’s charset alignment, which is exactly why your site went down. We need this base setting to build our fix on.

Now let’s diagnose and fix the root issues:

1. Verify Your Database’s Actual Charset & Collation

Log into your database (via phpMyAdmin, CLI, or your preferred tool) and run these checks:

  • Check the database-wide settings:
    SHOW CREATE DATABASE your_database_name;
    
  • Check a specific table that stores those problematic strings:
    SHOW CREATE TABLE your_table_name;
    
    Look for CHARSET= and COLLATE= values. Ideally, they should match utf8mb4 (better than plain utf8 for full Unicode support) with a collation like utf8mb4_unicode_ci. If they’re set to something else (like latin1), that’s a major culprit for the garbled text.

2. Align PHP & Web Server Charsets

  • PHP: Open your php.ini file and make sure this line is set (and not commented):
    default_charset = "UTF-8"
    
    Restart your PHP service after making this change.
  • Web Server:
    • For Nginx: Add or confirm this line in your site’s config:
      charset utf-8;
      
    • For Apache: Add this to your .htaccess or server config:
      AddDefaultCharset UTF-8
      
    Restart the web server afterward to apply the change.

3. Ensure Your HTML Tells Browsers to Use UTF-8

Double-check your site’s main template has this meta tag early in the <head> section:

<meta charset="UTF-8">

This prevents browsers from guessing the wrong encoding and mangling your text.

4. Fix Existing Garbled Data (If Needed)

If your database already has messed-up strings (like geëet instead of geëet), this usually means data was stored with the wrong charset. Always back up your database first! Then try these steps:

  1. Convert the table’s charset to utf8mb4:
    ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  2. If that doesn’t fix it, you might need to "recode" the data (works if data was stored as latin1 but should be utf8):
    ALTER TABLE your_table_name CHANGE column_name column_name TEXT CHARACTER SET latin1;
    ALTER TABLE your_table_name CHANGE column_name column_name TEXT CHARACTER SET utf8mb4;
    
    Replace column_name with the actual column storing the problematic text.

5. Double-Check Database Connection Charset

Even with DB_CHARSET set, some systems need an extra nudge to enforce the connection charset. Add this line right after your database connection code:

// For mysqli
mysqli_set_charset($connection, 'utf8mb4');

// For PDO
$pdo->exec("SET NAMES utf8mb4");

This ensures the link between your code and database uses the correct encoding.

Take it step by step, test after each change, and you’ll have those special characters displaying correctly again in no time!

内容的提问来源于stack exchange,提问作者WernerB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:06:02