【问题标题】:replace english quotes to german quotes将英文引号替换为德文引号
【发布时间】:2011-06-03 08:47:05
【问题描述】:

有没有办法实现德语引号(所谓的“Gänsefüßchen”)

„ („) and “ (“)

在一个函数中转换像

这样的英文引用字符串

我说“你好”

我说“你好”

&bdquo 应该只应用在字符串的开头,&ldquo 应用在字符串的结尾。

【问题讨论】:

    标签: php internationalization replace


    【解决方案1】:

    怎么样:

    $input  = 'I say "Hallo".';
    $output = preg_replace('/"(.*?)"/', '„$1“', $input);
    

    它将所有偶数引号替换为„“

    【讨论】:

      【解决方案2】:

      这是功能,经过测试并且工作正常。

      注意: &bdquo 应用于字符串的开头, &rdquo 仅应用于字符串的末尾。 (hsz 的解决方案不遵循该规则)

      function germanquotes($text){
          $size = strlen($text);
          $i=0;
          $replace = array();
          $replace['one'] = array();
          $replace['two'] = array();
          while($i < $size)
          {
              if($text[$i] == '"')
              {
                  if($text[$i-1] == " " || empty($text[$i-1]))
                  {
                          $replace['one'][] = $i;
                  }
                  elseif($text[$i+1] == " " || empty($text[$i+1]))
                  {
                      $replace['two'][] = $i;
                  }
              }
      
              $i++;
          }
      
          $y = 0;
          $it = 0;
          foreach($replace['one'] as $ghh)
          {
              $text = substr_replace($text, '&bdquo;', ($ghh+$y), 1);
              $y += 6;
              $it++;
          }
      
          $to=0;
          $i=1;
          $u=1;
          foreach($replace['two'] as $ghhd)
          {
              $text = substr_replace($text, '&rdquo;', ($ghhd-1+$to+((8*$i)-($u*1))), 1);
              $i++;
              $u +=2;
              $to +=6;
          }
      
          return $text;
      }
      

      测试:

      echo(germanquotes('I am "glad" to write "functions" for "stackoverflow" users'));
      

      【讨论】:

      • :D echo(germanquotes('我也是,因为它的工作原理是“über-fine”!'));
      【解决方案3】:

      你可以存储你所在的替换“状态”。首先你总是用&amp;bdquo替换一个引号,然后你设置一个标志,如果那个标志为真,你用&amp;rdquo替换一个引号,然后你转国旗关闭。重复。

      【讨论】:

        【解决方案4】:

        您也可以使用 CSS 属性 quotes

        quotes: "„" "“" "‚" "‘";
        

        Example

        【讨论】:

          【解决方案5】:

          假设您总是在“要替换的”前面有一个空格,您可以这样做

          str_replace('\"', '„', $input);

          【讨论】:

          • 但上面的答案更好;)
          猜你喜欢
          • 2021-02-25
          • 2018-11-18
          • 1970-01-01
          • 2017-12-29
          • 2017-09-07
          • 1970-01-01
          • 1970-01-01
          • 2019-07-07
          • 1970-01-01
          相关资源
          最近更新 更多