Skip to content

Instantly share code, notes, and snippets.

Forked from pjdietz/convertToAscii.php
Created August 22, 2021 19:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
PHP Functions for converting string to ASCII
* Normalize non-ASCII characters to ASCII counterparts where possible.
* @param string $str
* @return string
function squashCharacters($str)
static $normalizeChars = null;
if ($normalizeChars === null) {
$normalizeChars = array(
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'Ae',
'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',
'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I',
'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O',
'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'ae',
'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e',
'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i',
'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o',
'ù'=>'u', 'ú'=>'u', 'û'=>'u',
'Š'=>'S', 'š'=>'s', 'ś' => 's',
'Ž'=>'Z', 'ž'=>'z',
return strtr($str, $normalizeChars);
* Convert all fields in $item to ASCII.
* Do this by first normalizing the characters (á -> a, ñ -> n, etc.). If any
* non-ASCII characters remain, replace with a default value.
* @param array|object $item Array or object containing fields to convert
* @param array|object $template Contains template fields
* @param string $default Value to use when field not present in $template
* @param array $ignore Optional list of fields to ignore.
function convertFieldsToAscii(&$item, $template=null, $default='', array $ignore=null)
foreach ($item as $field => &$value) {
// Skip fields in the $ignore array.
if ($ignore && in_array($field, $ignore)) {
// Normalize non-ASCII characters with ASCII counterparts.
$value = squashCharacters($value);
// Replace fields that contain non-ASCII characters with a default.
if (mb_convert_encoding($value, 'ascii') !== $value) {
// If template is provided, use the template field, if set.
if ($template) {
if (is_object($template) && isset($template->{$field})) {
$value = $template->{$field};
} elseif (is_array($template) && isset($template[$field])) {
$value = $template[$field];
} else {
$value = $default;
} else {
$value = $default;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment