沃梦达 / 编程问答 / php问题 / 正文

php str_replace 不使用特殊字符

Php str_replace not working with special chars(php str_replace 不使用特殊字符)

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

问题描述

为什么没有按预期工作:

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

结果:

Fédération Camerounaise de Football"

我希望有:

喀麦隆足球联合会"

解决方案

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

您所要做的就是 utf8_decode('Fédération Camerounaise de Football').

更新:

您将看到 Fédération Camerounaise de Football 作为输出,因为您在 UTF-8 中双重传递数据.

观察:

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

输出:

<块引用><块引用>

Fédération Camerounaise de Football

现在,如果您告诉浏览器您使用的是 UTF-8,它应该直接显示内容:

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

输出:

<块引用><块引用>

喀麦隆足球联合会

完美.

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

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

输出:

<块引用><块引用>

Fédération Camerounaise de Football

真是一团糟.让我们看看是否可以用 str_replace 解决这个问题,让情况变得更糟:

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

输出:

<块引用><块引用>

Fédération Camerounaise de Football

如您所见,我们修复"了它.有点.这就是你正在做的事情.你正在将 é 转换为 é,即使你没有看到这一点,因为你的编辑器不会让您会看到编码背后的真实符号,但浏览器会.

让我们用 ASCII 再试一次:

file5.php 以 ASCII 格式保存:

输出:

<块引用><块引用>

喀麦隆足球联合会

魔法!浏览器现在得到了一切.但真正的解决方案是什么?好.如果你的 PHP 文件中有一个硬编码的字符串,那么你应该简单地编写 Fédération Camerounaise de Football 而不是把该死的东西放错了.但是,如果您从另一个文件或数据库中获取它,您应该参加以下两门课程之一:

  1. 使用 utf8_decode() 将您获取的数据转换为您想要的输出.

  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 不使用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php str_replace 不使用特殊字符

基础教程推荐