dongma6326 2013-07-16 04:42
浏览 57
已采纳

如何使用PHP截断字符串中的非ascii字符

I have a following string as a Filename

$string = 'recyclage plétre francin.jpg';

and tried with following code

echo preg_replace('/[^a-z0-9|^.]/i', '_', iconv("UTF-8","ISO-8859-1//TRANSLIT",$string));

as there is a special (non-ascii) character in filename it creates junk character while working with file uploading using PHP.

What I want is that replace any unicode (non-ascii) character with specific Ascii character. I want to keep all supported Ascii characters and remove non-ascii characters. I also want to keep / or \ slashes because of directory separators in filename where a root path will be given.

Edit: (below is not solved)

I am having a issue with recyclage plƒtre francin.JPG please the f character which displays output like recyclage pl and it had truncated .JPG. Actually file name was recyclage plâtre francin and when I was debugging it has shown recyclage plƒtre francin.JPG and rest is written just after that. Any Idea?

When I am trying to convert tri et recyclage du plâtre but when at the reading it shows tri et recyclage du plâtre and after conversion it shows tri et recyclage du pl^atre.

Any help will be appreciated.

  • 写回答

4条回答 默认 最新

  • dpvm7231 2013-07-16 09:41
    关注

    Here is a solution to my question. Finally I could able to see the conversion. Some Unicode characters are replaced with some Ascii characters. But after all everything is now working fine.

    function toASCII($str)
    {
        $accent   = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕƒ';
        $noaccent = 'SOZsozYYuaaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRra';
        $string = strtr(utf8_decode($string),utf8_decode($accent),$noaccent);
        return strtr($string, $accent, $noaccent);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab