【问题标题】:str_replace not replacing apostrophesstr_replace 不替换撇号
【发布时间】:2014-07-22 04:16:05
【问题描述】:

我在这里阅读了一个不同的问题,解决了同样的问题,但我似乎无法弄清楚这里发生了什么。我正在尝试用 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。此情况并非如此。我能够在屏幕上打印出字符串并正确设置,只是没有格式化。感谢您迄今为止在这个问题上的活动!

【问题讨论】:

  • 只是为了确保,因为在最初的例子中是$question,然后在你处理它的$clue_question时,它们的内容是否相同?

标签: php str-replace


【解决方案1】:

请注意,&amp;#039; 等是 HTMLism,用于在纯文本中表达特定于字符集的图形的方法。要让 str_replace() 理解 &amp;#039;,您需要将 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() 来限制您的字符集可能会产生意想不到的后果,例如删除您不知道的特殊字符。小心轻放。彻底测试。使用浴室后务必洗手。

【讨论】:

  • 成功了。谢谢你教我一些新东西。我不知道为什么它不能正常工作(我以前从未遇到过这个问题)。我将继续小心处理:p
【解决方案2】:

您似乎正在定义一个名为$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);
?>

【讨论】:

    【解决方案3】:

    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
    

    【讨论】:

      【解决方案4】:

      这是一个非常古老的问题,但这可能对其他人有用。在我的例子中,可怕的单引号(撇号)是由 MS 单词生成的,这是一个替换它的函数(以及其他 MS 单词可憎)

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-30
        • 2013-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多