/** * Fix MFC mojibake by deterministic replacement → real emoji. * SAFE, idempotent, fast. */ function repair_text(string $s): string { if ($s === '') return $s; // 1) remove zero-width & invisible junk $s = preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $s) ?? $s; // 2) deterministic mojibake → emoji map (MFC specific) static $map = [ // hearts / love '♥' => '♥', 'â¤' => '❤️', 'â¤ï¸' => '❤️', // devil / naughty '😈' => '😈', // fire / hot '🔥' => '🔥', // peach / ass 'ðŸ‘' => '🍑', // water / wet '💦' => '💦', // camera / video '📽' => '📽', '🎬' => '🎬', // sparkle / stars '✨' => '✨', '🌟' => '🌟', // house / goals 'ðŸ¡' => '🏡', // paw / pets 'ðŸ¾' => '🐾', // dice / random '🎲' => '🎲', ]; $s = strtr($s, $map); // 3) fix broken apostrophes & quotes $s = str_replace( ['​', '’', '“', 'â€'], ['', '’', '“', '”'], $s ); // 4) normalize spaces $s = preg_replace('/\s+/u', ' ', $s) ?? $s; return trim($s); }