-
-
Save codler/1500962 to your computer and use it in GitHub Desktop.
This simple function will remove any non-ASCII character. Feel free to fork and extend!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove any non-ASCII characters and convert known non-ASCII characters | |
* to their ASCII equivalents, if possible. | |
* | |
* @param string $string | |
* @return string $string | |
* @author Jay Williams <myd3.com> | |
* @license MIT License | |
* @link http://gist.github.com/119517 | |
*/ | |
function convert_ascii($string) | |
{ | |
// Replace Single Curly Quotes | |
$search[] = chr(226).chr(128).chr(152); | |
$replace[] = "'"; | |
$search[] = chr(226).chr(128).chr(153); | |
$replace[] = "'"; | |
// Replace Smart Double Curly Quotes | |
$search[] = chr(226).chr(128).chr(156); | |
$replace[] = '"'; | |
$search[] = chr(226).chr(128).chr(157); | |
$replace[] = '"'; | |
// Replace En Dash | |
$search[] = chr(226).chr(128).chr(147); | |
$replace[] = '--'; | |
// Replace Em Dash | |
$search[] = chr(226).chr(128).chr(148); | |
$replace[] = '---'; | |
// Replace Bullet | |
$search[] = chr(226).chr(128).chr(162); | |
$replace[] = '*'; | |
// Replace Middle Dot | |
$search[] = chr(194).chr(183); | |
$replace[] = '*'; | |
// Replace Ellipsis with three consecutive dots | |
$search[] = chr(226).chr(128).chr(166); | |
$replace[] = '...'; | |
// Apply Replacements | |
$string = str_replace($search, $replace, $string); | |
// Remove any non-ASCII Characters | |
$string = preg_replace("/[^\x01-\x7F]/","", $string); | |
return $string; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert known non-ASCII latin1 characters to their ASCII equivalents, if possible. | |
* | |
* This code have been modified by Han Lin Yap by combining functions from | |
* http://gist.github.com/119517 and http://phpxref.com/xref/wiclear/inc/classes/utf8_helper.class.php.source.txt | |
* | |
* @param string $string | |
* @return string $string | |
* @version 1.0 (2011-12-15) | |
*/ | |
function fix_latin1_ascii($string) { | |
$conv = array( | |
// Replace Single Curly Quotes | |
chr(194).chr(145) => "'", | |
chr(194).chr(146) => "'", | |
// Replace Smart Double Curly Quotes | |
chr(194).chr(147) => '"', | |
chr(194).chr(148) => '"', | |
// Replace Bullet | |
chr(194).chr(149) => '*', | |
// Replace En Dash | |
chr(194).chr(150) => '--', | |
// Replace Em Dash | |
chr(194).chr(151) => '---', | |
// Replace Middle Dot | |
chr(194).chr(183) => '*', | |
// Replace Ellipsis with three consecutive dots | |
chr(194).chr(133) => '...', | |
); | |
return str_replace(array_keys($conv), array_values($conv), $string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment