Skip to content

Instantly share code, notes, and snippets.

@martinheidegger
Last active December 24, 2015 15:59
Show Gist options
  • Save martinheidegger/6825241 to your computer and use it in GitHub Desktop.
Save martinheidegger/6825241 to your computer and use it in GitHub Desktop.
Including a php file with a different encoding can be very frustrating. Here is a method to get that done!
define("MB_INCLUDE_BLOCK", 3000);
function mb_echo($str, $to_encoding, $from_encoding) {
$len = mb_strlen($str);
for($pos=0; $pos < $len;) {
$part = mb_strcut($str, $pos, min($len-$pos, MB_INCLUDE_BLOCK), $from_encoding);
$pos += strlen($part);
$converted = mb_convert_encoding($part, $to_encoding, $from_encoding);
echo $converted;
flush();
}
}
function mb_include($file, $to_encoding, $from_encoding) {
flush();
ob_start();
include($file);
$str = ob_get_contents();
ob_end_clean();
mb_echo($str, $to_encoding, $from_encoding);
}
@martinheidegger
Copy link
Author

Example: mb_include("other.php", "UTF-8", "Shift-JIS");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment