duandeng2011 2014-08-23 18:32
浏览 180
已采纳

Php str_replace无法使用特殊字符

why isn't this working as expected:

 echo str_replace("é","é","Fédération Camerounaise de Football");

result:

"Fédération Camerounaise de Football"

i'm expecting to have:

"Fédération Camerounaise de Football"
  • 写回答

3条回答 默认 最新

  • dongrong5189 2014-08-23 18:53
    关注

    You are doing it wrong. This string is not incorrect and in need of replacement, it is simply encoded with UTF-8.

    All you have to do is utf8_decode('Fédération Camerounaise de Football').

    Update:

    You are seeing Fédération Camerounaise de Football as output because you are double passing your data in UTF-8.

    Observe:

    file1.php saved in UTF-8 format:

    <?php
        echo "Fédération Camerounaise de Football";
    

    Output:

    Fédération Camerounaise de Football

    Now, if you tell the browser you are using UTF-8, it should display the content straight:

    file2.php saved in UTF-8 format:

    <?php
        header('Content-Type: text/html; charset=utf-8');
        echo "Fédération Camerounaise de Football";
    

    Output:

    Fédération Camerounaise de Football

    Perfect.

    Howover, you are doing things even worse. You have an UTF-8 encoded string, and is encoding it again, by writing it to a UTF-8 encoded file.

    file3.php saved in UTF-8 format:

    <?php
        echo "Fédération Camerounaise de Football";
    

    Output:

    Fédération Camerounaise de Football

    What a mess. Let's make it worse by seeing if we can fix this with str_replace:

    file4.php saved in UTF-8 format:

    <?php
        echo str_replace("é","é","Fédération Camerounaise de Football");
    

    Output:

    Fédération Camerounaise de Football

    As you can see, we "fixed" it. Sort of. Thats what you are doing. You are transforming é into é, even though you are not seeing this because your editor won't let you see the real symbols behind the encoding, but the browser does.

    Let's try this again with ASCII:

    file5.php saved in ASCII format:

    <?php
        echo str_replace("é","é","Fédération Camerounaise de Football");
    

    Output:

    Fédération Camerounaise de Football

    Magic! The browser got everything right now. But whats the real solution? Well. If you have a string hardcoded in your PHP file, then you should simply write Fédération Camerounaise de Football instead of placing the god damn thing wrong. But if you are fetching it from another file or a database, you should take one of the two courses:

    1. Use utf8_decode() to transform the data you fetch into your desired output.

    2. Don't transform anything and use header('Content-Type: text/html; charset=utf-8'); to tell the browser you are printing content in UTF-8 format, so it will display things correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys