gpt4 book ai didi

PHP 和 字符编码问题

转载 作者:可可西里 更新时间:2023-10-31 22:07:40 24 4
gpt4 key购买 nike

我遇到一个问题,PHP (5.2) 无法在字符串中找到字符 'Â',尽管它显然存在。

我意识到潜在的问题与字符编码有关,但不幸的是我无法控制源内容。我以 UTF-8 格式收到它,这些字符已经在字符串中。

我只想将它从字符串中删除。 strpos()、str_replace()、preg_replace()、trim()等无法正确识别。

我的字符串是这样的:

"Â  Â  Â  A lot of couples throughout the World "

如果我这样做:

$string = str_replace('Â','',$string);

我明白了:

"� � � A lot of couples throughout the World"

我什至在 str_replace 之前尝试了 utf8_encode() 和 utf8_decode(),但没有成功。

解决方案是什么?我一直在投入我能找到的一切......

最佳答案

$string = str_replace('Â','',$string);

这个“”是如何编码的?如果您的脚本文件保存为 iso-8859-1,则字符串 'Â' 被编码为单字节序列 xC2,而 (/one) utf-8 表示形式为 xC3 x82。 php 的 str_replace() 在字节级别上工作,即它只“知道”单字节字符。

参见 http://docs.php.net/intro.mbstring

关于PHP 和 字符编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com