php - str_replace 不替换撇号

标签 php str-replace

我在这里读到了一个不同的问题来解决同样的问题,但我似乎不知道这里发生了什么。我正在尝试用 str_replace 替换代码中的撇号,但它不起作用。我有一个字符串要测试:

$clue_question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";

然后一些字符串替换及其结果:

$new_string = str_replace("I", "a", $clue_question)."<br />";
//af you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$another_new_string = str_replace("'", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another_new_string = str_replace("&#039;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$sdf_another_new_string = str_replace("&#096;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$sdf_another_new_string_sdf = str_replace("&#239;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another = str_replace("’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another = str_replace("\’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

我已经尝试过 ASCII 等效项,我已经尝试过转义它们,我已经尝试过它们正常。我不知道为什么我的字符串中的单撇号(或者可能是单引号)没有被替换。我的语法很好,因为第一个 str_replace() 函数工作正常。我用谷歌搜索了一下,在检查了这里的另一个问题后,我唯一能想到的是这是我的 PHP 版本。我运行了 phpinfo() 并且版本为 5.2.17,所以我假设这不是问题。

有什么想法吗?谢谢。


编辑

抱歉,我最初有一个类型,并将初始变量设置为 $question 而不是 $clue_question。此情况并非如此。我可以在屏幕上打印出字符串并正确设置,只是没有格式化。感谢您到目前为止就这个问题所做的事件!

最佳答案

请注意,' 等是 HTMLism,用于在纯文本中表达特定于字符集的数字的方法。为了让 str_replace() 理解 ',您需要将 HTML 转换为真实的字符集。

我真的无法说出为什么您的非 HTML 尝试失败,但如果您的输入数据以您不想处理的字符集进行编码,那么也许您需要转换为更容易管理的内容。

<?php

$q = "If you’re rowin’ the Rhone\n";

print "Original:  " . $q;
print "Converted: " . iconv("UTF-8","ASCII//TRANSLIT",$q);

对我来说,在无法正确显示 UTF-8 的 xterm 中,这会得到以下结果:

Original:  If youâre rowinâ the Rhone
Converted: If you're rowin' the Rhone

如果您可以将输入降级为始终按预期运行的内容,那么您可能会更轻松地操纵它。

请注意,这不是处理特殊字符的通用解决方案。使用 iconv() 限制字符集可能会产生意想不到的后果,例如删除您不知道的特殊字符。小心轻放。彻底测试。使用浴室后务必洗手。

关于php - str_replace 不替换撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987874/

相关文章:

php - Wordpress 导航菜单页面项目随机消失

php - 如何使用 node_load 并呈现上下文链接

jQuery:将 $(货币符号)替换为另一个货币符号

php - Sql 不接受 php 中的值

php - 从一个网页重定向到另外两个网页

php - Symfony : The service . .. 依赖于不存在的参数 kernel.secret

php - PHP str_replace 是否有超过 13 个字符的限制?

PHP 查询将结果 1 更改为 YES

php - 用 str_replace 只替换字符串的第一个字符?

string - Python Pandas : How to replace string contain "?"