Skip to content

Instantly share code, notes, and snippets.

@codfish
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codfish/874337ea4dc342249d66 to your computer and use it in GitHub Desktop.
Save codfish/874337ea4dc342249d66 to your computer and use it in GitHub Desktop.
This function strips incompatible iso characters from ms office documents. If no ascii conversion is found the character is ignored.
/**
* This function strips incompatible iso characters
* from ms office documents. If no ascii conversion is found
* the character is ignored.
*
* @author hannuraina
* @param array|string $replace value(s) to replace
* @return array|string $items
*/
function msword_to_ascii($replace) {
if (empty($replace)) {
return $replace;
}
// convert single values to array
$items = !is_array($replace) ? [$replace] : $replace;
array_walk($items, function(&$item, $key) {
if (is_string($item)) {
$value = @iconv('UTF-8', 'ASCII//TRANSLIT', trim($item));
// word/excel generated characters will fail with
// default iconv implentation on linux. target
// windows input charset.
$item = $value ? $value : @iconv('Windows-1252', 'ASCII//TRANSLIT//IGNORE', trim($item));
}
});
// determine return type from original input
return is_array($replace) ? $items : reset($items);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment