Remove alt-codes from string

2019-06-12 10:08发布

问题:

Is there a possibility to replace alt-code-characters with "" in PHP?

Example input: Hell©ó

Output should be: Hello

I've tried preg_replace("/[^A-Za-z]+/i", "", $string);

Edit:

The problem is that I have to iconv the string before because I need letters like "á" to be replaced with alphanumeric ones, they should not just be deleted because I need to compare the string later. preg_replace would change "Ke©álo" to "Kelo". I need it to put out "Kealo".

回答1:

This can be achieved using 2 seperate functions.

strtr() first, then preg_replace() following strtr() in that order while using your present preg_replace("/[^A-Za-z]+/i", "", $string); code.

Sidenote: You can later add to the array if needed. 'é'=>'e' as an example.

$string = "Ke©álo";

// borrowed from http://stackoverflow.com/a/3373364/
$unwanted_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', 'ß'=>'Ss', 'à'=>'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' );
$string = strtr( $string, $unwanted_array );

$newstring = preg_replace("/[^A-Za-z]+/i", "", $string);

echo $newstring; // echo'd Kealo

"I need it to put out "Kealo"."



回答2:

You must need to add u (unicode) modifier.

preg_replace("/[^A-Za-z]+/u", "", $string);