replace english quotes to german quotes

梦想与她 提交于 2019-12-07 06:08:54

问题


is there a way to implement german quotes (so-called 'Gänsefüßchen')

„ („) and “ (“)

in a function to convert english-quoted strings like

I say "Hallo"

to

I say „Hallo“

the &bdquo should only apply at the beginning, the &ldquo at the end of an string.


回答1:


Here is the function, tested and works fine.

Note: the &bdquo applies only at the beginning, the &rdquo only at the end of an string. (hsz's solution isn't following that rule)

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;
}

Test:

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



回答2:


What about:

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

It replaces all even amount of quotes into „“.




回答3:


You could store the replacement "state" you are in. First you always replace a quote with &bdquo, then you set a flag and if that flag is true, you replace a quote with &rdquo and then you turn the flag off. Repeat.




回答4:


You can do it also with CSS property quotes:

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

Example




回答5:


Asuming you always have a space infront of the " you want to replace you could do it like this

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



来源:https://stackoverflow.com/questions/6224893/replace-english-quotes-to-german-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!