2011-06-03 65 views
4

有沒有辦法來實現德國引號(所謂的「Gänsefüßchen」)取代英文引號德國報價

„ („) and “ (「) 

在函數中英文引號字符串轉換像

我說「喂」

我說「你好」

的& bdquo應僅適用於初,在字符串末尾的& ldquo。

回答

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')); 
+0

:d回聲(germanquotes(「我也是,你是,作爲它的作品「über-fine」!')); – cukabeka 2011-06-09 16:54:44

2

您可以存儲您所在的替換「狀態」。首先,您總是用&bdquo替換報價,然後設置一個標誌,如果該標誌爲真,則用&rdquo替換報價,然後關閉標誌。重複。

5

什麼:

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

它取代所有甚至報價金額爲„「

2

你可以用CSS屬性quotes做到這一點也:

quotes: "„" "「" "‚" "‘"; 

Example

1

Asuming你總是有一個空間盈的「要替換,你可以做這樣的

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

+0

但上面的回答是更好;) – Touchpad 2011-06-03 09:38:46