str_replace不替换撇号

时间:2014-06-02 05:33:47

标签: 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()函数正常工作。我已经用Google搜索了,我唯一能想到的就是我在这里查看了另一个问题后的PHP版本。我运行了phpinfo(),我有5.2.17版本,所以我假设这不是问题。

有什么想法吗?谢谢。


修改

我很抱歉,我最初有一个类型,并且初始变量为$ question而不是$ clue_question。此情况并非如此。我能够在屏幕上打印出字符串并正确设置,它只是不格式化。感谢迄今为止关于这个问题的活动!

4 个答案:

答案 0 :(得分:3)

请注意,&#039;等是HTMLisms,用于在仅文本的内容中表达特定于字符集的数字的方法。要让str_replace()了解&#039;,您需要将HTML转换为真实的字符集。

我无法确定为什么非HTML尝试失败,但如果您的输入数据是以您不想处理的字符集编码的,那么您可能需要转换为更容易管理的内容。 / p>

<?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()限制字符集可能会产生意想不到的后果,例如删除您不知道的特殊字符。小心轻放。彻底测试。使用浴室后务必洗手。

答案 1 :(得分:0)

您似乎正在定义一个名为$question的变量,并对名为$clue_question的变量执行替换。我刚刚测试了以下内容并且工作正常。

<?php
$question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";
$another_new_string = str_replace("’", "VB", $question);
var_dump($another_new_string);
?>

答案 2 :(得分:0)

str_replace()函数中使用$question变量而不是$clue_question。变量名称错误。

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

echo $yet_another = str_replace("’", "sdgd", $question)."<br />";

<强>输出:

If yousdgdre rowinsdgd the Rhone from start to finish, you begin in this mountain range

答案 3 :(得分:0)

这是一个非常老的问题,但这可能对其他人有用。在我的案例中,可怕的单引号(撇号)是由MS word生成的,这是一个代替它(和其他MS word令人憎恶的)的函子。

function msword_conversion($str)
{
    $str = str_replace(chr(130), ',', $str);    // baseline single quote
    $str = str_replace(chr(131), 'NLG', $str);  // florin
    $str = str_replace(chr(132), '"', $str);    // baseline double quote
    $str = str_replace(chr(133), '...', $str);  // ellipsis
    $str = str_replace(chr(134), '**', $str);   // dagger (a second footnote)
    $str = str_replace(chr(135), '***', $str);  // double dagger (a third footnote)
    $str = str_replace(chr(136), '^', $str);    // circumflex accent
    $str = str_replace(chr(137), 'o/oo', $str); // permile
    $str = str_replace(chr(138), 'Sh', $str);   // S Hacek
    $str = str_replace(chr(139), '<', $str);    // left single guillemet
// $str = str_replace(chr(140), 'OE', $str);   // OE ligature
    $str = str_replace(chr(145), "'", $str);    // left single quote
    $str = str_replace(chr(146), "'", $str);    // right single quote
// $str = str_replace(chr(147), '"', $str);    // left double quote
// $str = str_replace(chr(148), '"', $str);    // right double quote
    $str = str_replace(chr(149), '-', $str);    // bullet
    $str = str_replace(chr(150), '-–', $str);    // endash
    $str = str_replace(chr(151), '--', $str);   // emdash
// $str = str_replace(chr(152), '~', $str);    // tilde accent
// $str = str_replace(chr(153), '(TM)', $str); // trademark ligature
    $str = str_replace(chr(154), 'sh', $str);   // s Hacek
    $str = str_replace(chr(155), '>', $str);    // right single guillemet
// $str = str_replace(chr(156), 'oe', $str);   // oe ligature
    $str = str_replace(chr(159), 'Y', $str);    // Y Dieresis
    $str = str_replace('°C', '&deg;C', $str);    // Celcius is used quite a lot so it makes sense to add this in
    $str = str_replace('£', '&pound;', $str);
    $str = str_replace("'", "'", $str);
    $str = str_replace('"', '"', $str);
    $str = str_replace('–', '&ndash;', $str);

    return $str;
} 

来源:https://www.php.net/manual/en/function.str-replace.php

相关问题