Вот функция, протестированная и отлично работающая.
Примечание: применяется & 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, '„', ($ghh+$y), 1);
$y += 6;
$it++;
}
$to=0;
$i=1;
$u=1;
foreach($replace['two'] as $ghhd)
{
$text = substr_replace($text, '”', ($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'));