php str_replace不工作与特殊字符 [英] Php str_replace not working with special chars

查看:220
本文介绍了php str_replace不工作与特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不是按预期工作:

  echo str_replace(é,é,Fà ©décationCamerounaise de Football); 

结果:

 code>FédíféCamerounaise de Football

 FédérationCamerounaise de Football


解决方案

你做错了。这个字符串不正确,需要替换,它只是用UTF-8编码。



所有你需要做的是 utf8_decode('



更新:



您会看到FédémentCamerounaise de Football 作为输出,因为您在UTF-8中双重传送了您的数据。



观察:



以UTF-8格式保存的file1.php

 <?php 
echoFédérationCamerounaise de Football;

输出:



足球比赛



,如果你告诉浏览器你使用UTF-8,它应该直接显示内容:



file2.php以UTF-8格式保存:

 <?php 
header('Content-Type:text / html; charset = utf-8 ');
echoFédérationCamerounaise de Football;

输出:



足球比赛



>

但是,你做的事情更糟。您有一个UTF-8编码的字符串,并通过将其写入一个UTF-8编码的文件,对其进行编码。



file3.php保存为UTF -8格式

 <?php 
echoFédécesCamerounaise de Football ;

输出:



Fêƒñ©足球比赛



一团糟。让我们看看是否可以通过 str_replace



修复file4.php UTF-8格式:

 <?php 
echo str_replace(é, é,FédéationCamerounaise de Football);

输出:



Camerounaise de Football足球



你可以看到,我们固定了。有点。 这是您正在做的事。。您正在将é转换为é您没有看到这一点,因为您的编辑器不会让您看到编码后面的真实符号



再次使用ASCII:



file5.php以ASCII格式保存

 <?php 
echo str_replace(Ã,é,Fédicure Camerounaise de Football);

输出:



足球比赛



浏览器现在得到一切。但真正的解决方案是什么?好。如果你有一个字符串硬编码在你的PHP文件,那么你应该简单地写FédérationCamerounaise de Football ,而不是把上帝的东西错了。但是,如果您从其他文件或数据库中提取它,则应该选择以下两个课程之一:


  1. 使用<$


  2. 不要转换任何内容并使用 header('Content-Type:text / html; charset = utf-8'); 告诉浏览器您正在以UTF-8格式打印内容,



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"

解决方案

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.

这篇关于php str_replace不工作与特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆