从字符串中删除替代代码 [英] Remove alt-codes from string
问题描述
有没有可能在 PHP 中用 "" 替换 alt-code-characters?
示例输入:Hell©ó
输出应该是:你好
我试过 preg_replace("/[^A-Za-z]+/i", "", $string);
问题是我必须在之前对字符串进行 iconv,因为我需要将á"之类的字母替换为字母数字,而不应该只是删除它们,因为我需要稍后比较字符串.preg_replace 会将Ke©álo"更改为Kelo".我需要它来推出Kealo".
这可以使用 2 个单独的函数来实现.
strtr()
先是 preg_replace()
跟随 strtr()
在使用你现在的 preg_replace("/[^A-Za-z]+/i", "", $string);
代码.
旁注:如果需要,您可以稍后添加到数组中.'é'=>'e'
为例.
$string = "Ke©álo";//借自 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);回声 $newstring;//echo'd Kealo
<块引用>
我需要它来推出Kealo".
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".
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"."
这篇关于从字符串中删除替代代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!