简体   繁体   English

Php str_replace无法使用特殊字符

[英]Php str_replace not working with special chars

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. 此字符串不正确,需要替换,它只是用UTF-8编码。

All you have to do is utf8_decode('Fédération Camerounaise de Football') . 所有你需要做的就是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. 你会看到Fédération Camerounaise de Football作为输出,因为你是以UTF-8双倍传递你的数据。

Observe: 注意:

file1.php saved in UTF-8 format: file1.php以UTF-8格式保存:

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

Output: 输出:

Fédération Camerounaise de Football FédérationCasrounaise de Football

Now, if you tell the browser you are using UTF-8, it should display the content straight: 现在,如果您告诉浏览器您使用的是UTF-8,它应该直接显示内容:

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

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

Output: 输出:

Fédération Camerounaise de Football FédérationCamerounaisede 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. 您有一个UTF-8编码的字符串,并通过将其写入UTF-8编码文件再次对其进行编码。

file3.php saved in UTF-8 format: file3.php以UTF-8格式保存:

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

Output: 输出:

Fédération Camerounaise de Football 来自Camerounaise de Football的Fé©dé

What a mess. 真是一团糟。 Let's make it worse by seeing if we can fix this with str_replace : 让我们通过看看我们是否可以使用str_replace解决这个问题来加剧它:

file4.php saved in UTF-8 format: file4.php以UTF-8格式保存:

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

Output: 输出:

Fédération Camerounaise de Football FédérationCasrounaise 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: 让我们用ASCII再试一次:

file5.php saved in ASCII format: file5.php以ASCII格式保存:

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

Output: 输出:

Fédération Camerounaise de Football FédérationCamerounaisede 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. 如果你的PHP文件中有一个字符串硬编码,那么你应该简单地编写Fédération Camerounaise de Football而不是将该死的东西放错。 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. 使用utf8_decode()将您获取的数据转换为所需的输出。

  2. Don't transform anything and use header('Content-Type: text/html; charset=utf-8'); 不要转换任何东西并使用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. 告诉浏览器您正在以UTF-8格式打印内容,因此它将正确显示内容。

//edit after comment //评论后编辑

Fédération Camerounaise de Football is an UTF-8 encoded string so i don't know what input is not utf-8 encoded in your document but you have two options. Fédération Camerounaise de Football是一个UTF-8编码的字符串,因此我不知道您的文档中的输入不是utf-8编码,但您有两种选择。

  1. your input that are passed to str_replace is utf-8 but the characters that you have used in the functions to replace are ANSII or something else => not work - this means your document is not utf-8 - this is why uft8_decode works str_replace(ANSII, ANSII, CONVERT_TO_ANSII(UTF-8)) 您传递给str_replace的输入是utf-8但是您在要替换的函数中使用的字符是ANSII或其他东西=>不起作用 - 这意味着您的文档不是utf-8 - 这就是uft8_decode工作str_replace(ANSII, ANSII, CONVERT_TO_ANSII(UTF-8))

  2. your input is not utf-8 and your document is - so this would work str_replace(UTF-8, UTF-8, CONVERT_TO_UTF-8(ANSII)) 你的输入不是utf-8而你的文件是 - 所以这将工作str_replace(UTF-8, UTF-8, CONVERT_TO_UTF-8(ANSII))


str_replace works great with multibyte characters - your problem is not the function its is because you try to replace different encoding types. str_replace适用于多字节字符 - 您的问题不是它的功能,因为您尝试替换不同的编码类型。 instead of using a alternative function - i suggest you to fix the input that are passed to str_replace to utf-8 and make sure that your document is utf-8 encoded too. 而不是使用替代函数 - 我建议你修改传递给str_replaceutf-8的输入,并确保你的文档也是utf-8编码。

if your source only support non utf-8 encoding use utf8_encode to convert your input to utf-8 如果您的源仅支持非utf-8编码,请使用utf8_encode将输入转换为utf-8

http://php.net/manual/de/function.utf8-encode.php http://php.net/manual/de/function.utf8-encode.php

Check The following Code: 检查以下代码:

$chain="Fédération Camerounaise de Football";
$pattern = array("'é'");
$replace = array('é'); 
$chain = preg_replace($pattern, $replace, $chain);
echo $chain;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM