我有一个包含 £ 符号的 HTML 字符串,出于某种原因我无法替换它们。我假设这是一个编码问题,尽管我不知道如何解决。该站点使用 ISO-8859-1 进行编码
$str = '<span class="price">£89.99</span>';
var_dump(mb_detect_encoding($str, 'ISO-8859-1', true)); // outputs ISO-8859-1
echo str_replace(array("£","£"),"",$str); // nothing is removed
echo htmlentities($str); // the entire string is converted, including £ to £
有什么想法吗?
编辑
应该指出我想用 £
替换 £; - 我临时将 £
添加到要替换的项目数组中,以防它已经被转换
最佳答案
只是一个猜测,但有没有可能即使您的网站以 ISO-8859-1 编码输出,您实际的 *.php 文件也保存为 utf-8?我认为 str_replace 不能正确处理 utf-8 字符串。要测试它,请尝试:
str_replace(utf8_decode("£"),"£",utf8_decode($str));
是的,如果这可行,那么您的 *.php 文件将以 utf-8 编码保存。这意味着所有字符串常量都在 utf-8 中。可能值得将 IDE 中的默认编码切换为 ISO-8859-1
关于php - 无法用字符串中的 £ 替换 £,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8227771/