本文介绍了如何从PHP字符串中的字符中删除重音符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试从PHP字符串中的字符中删除重音符号,这是使字符串在URL中可用的第一步.I'm attempting to remove accents from characters in PHP string as the first step to making the string usable in a URL.我正在使用以下代码:$input = "Fóø Bår";setlocale(LC_ALL, "en_US.utf8");$output = iconv("utf-8", "ascii//TRANSLIT", $input);print($output);我期望的输出将是这样的:The output I would expect would be something like this:F'oo Bar但是,不是将带重音符号的音译转换为问号,而是将其替换为问号:However, instead of the accented characters being transliterated they are replaced with question marks:F?? B?r我在网上可以找到的所有内容都表明设置语言环境将解决此问题,但是我已经在这样做了.我已经检查了以下详细信息:Everything I can find online indicates that setting the locale will fix this problem, however I'm already doing this. I've already checked the following details:我设置的语言环境受服务器支持(包含在locale -a生成的列表中)服务器版本的iconv(包含在iconv -l生成的列表中)支持源编码和目标编码(UTF-8和ASCII)输入字符串是UTF-8编码的(已使用PHP的mb_check_encoding函数进行了验证,如墨卡托的回答)对setlocale的调用成功(返回'en_US.utf8'而不是FALSE)The locale I am setting is supported by the server (included in the list produced by locale -a)The source and target encodings (UTF-8 and ASCII) are supported by the server's version of iconv (included in the list produced by iconv -l)The input string is UTF-8 encoded (verified using PHP's mb_check_encoding function, as suggested in the answer by mercator)The call to setlocale is successful (it returns 'en_US.utf8' rather than FALSE)问题原因:服务器正在使用错误的iconv实现.它具有 glibc 版本,而不是必需的 libiconv 版本.The cause of the problem:The server is using the wrong implementation of iconv. It has the glibc version instead of the required libiconv version. phpinfo函数的输出中包含有关PHP使用的iconv实现的详细信息.Details about the iconv implementation that is used by PHP are included in the output of the phpinfo function.(我无法在与此项目一起使用的服务器上使用正确的iconv库重新编译PHP,因此下面我接受的答案是在没有iconv的情况下删除重音符号最有用的答案支持.)(I'm not able to re-compile PHP with the correct iconv library on the server I'm working with for this project so the answer I've accepted below is the one that was most useful for removing accents without iconv support.)推荐答案我认为这里的问题是您的编码将ä和å的符号视为与'a'不同.实际上,用于strtr的PHP文档提供了一个以丑陋的方式消除重音符号的示例:(I think the problem here is that your encodings consider ä and å different symbols to 'a'. In fact, the PHP documentation for strtr offers a sample for removing accents the ugly way :( http://ie2.php.net/strtr 这篇关于如何从PHP字符串中的字符中删除重音符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 02:50