Skip to content

Instantly share code, notes, and snippets.

@niquenen
Last active March 28, 2023 13:04
Show Gist options
  • Save niquenen/d06a55ddf11f4a08a421750c2ccb96b6 to your computer and use it in GitHub Desktop.
Save niquenen/d06a55ddf11f4a08a421750c2ccb96b6 to your computer and use it in GitHub Desktop.
PHP function to replace all characters with an ASCII equivalent.
<?php
/**
* @author niquenen
* @company H2V Solutions
* @created_at 2020-02-18 10:54:10
* @updated_by niquenen
* @updated_at 2022-11-04 15:16:13
*/
/**
* Replace all characters with an ASCII equivalent.
* This function requires `mbstring` and `iconv` libraries.
*
* @see https://stackoverflow.com/questions/1176904 How to remove all
* non printable characters
* in a string?
*
* @param string $str Original string converted.
* @param bool $printable Checks for any printable characters.
* @return string|null ASCII encoded string or null if the functions are not
* found or if a problem has occurred.
*/
function toAscii(string $str, bool $printable = false): ?string
{
$encoding = '';
if (!function_exists('mb_detect_encoding') || !function_exists('iconv')) {
return null;
}
else if ($printable) {
$str = preg_replace('/[\x00-\x1F\x7F]/u', '', $str);
}
$encoding = mb_detect_encoding($str, mb_detect_order(), true);
if ($encoding === false) {
return null;
}
else if ($encoding != 'ASCII') {
$str = iconv($encoding, 'ASCII//TRANSLIT', $str);
return $str === false ? null : preg_replace('#[^-\w]+#', '', $str);
}
return $str;
}
@niquenen
Copy link
Author

niquenen commented Apr 26, 2020

For more information about the question:

How to remove all non printable characters in a string?

I redirect to this post on Stack Overflow.

An example of the function can be found below (or on PHPSandbox):

<?php

$array = array(
    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A',
    'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C',
    'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I',
    'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
    'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U',
    'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'à'=>'a', 'á'=>'a', 'â'=>'a',
    'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e',
    'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
    'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o',
    'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u',
    'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ß'=>'ss'
);

// Returns: "ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞàáâãäåæçèéêëìíîïðñòóôõöøùúûýþÿß"
var_dump(implode('', array_keys($array)));
// Returns: "SsZzAAAAAAAECEEEEIIIINOOOOOOUUUUYThaaaaaaaeceeeeiiiidnoooooouuuythyss"
var_dump(toAscii(implode('', array_keys($array))));

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