<?
/* (C) 2004 by Folke Ashberg www.ashberg.de / folke@ashberg.de */

// ob_start('ob_b') to translate the php-output on the fly

function strtobsprache($in){
    
// alleinstehende konsonanten 'aussprechen'
    
$x="[[:space:]!?:\"'/.,_-]";
    
$in=preg_replace("#(^|$x)([bcdgpwt])($x|$)#""\\1\\2e\\3"$in);
    
$in=preg_replace("#(^|$x)([frslnm])($x|$)#""\\1e\\2\\3"$in);
    
$in=preg_replace("#(^|$x)([kh])($x|$)#""\\1\\2a\\3"$in);
    
$in=preg_replace("#(^|$x)([q])($x|$)#""\\1\\2u\\3"$in);
    
$in=preg_replace("#(^|$x)([j])($x|$)#""\\1\\2ott\\3"$in);
    
$in=preg_replace("#(^|$x)([v])($x|$)#""\\1\\2au\\3"$in);
    
$in=preg_replace("#(^|$x)([x])($x|$)#""\\1i\\2\\3"$in);
    
$in=preg_replace("#(^|$x)([y])($x|$)#""\\1\\2psilon\\3"$in);
    
$in=preg_replace("#(^|$x)([z])($x|$)#""\\1\\2ett\\3"$in);
    
$in=preg_replace("#(^|$x)([BCDGPWT])($x|$)#""\\1\\2E\\3"$in);
    
$in=preg_replace("#(^|$x)([FRSLNM])($x|$)#""\\1E\\2\\3"$in);
    
$in=preg_replace("#(^|$x)([KH])($x|$)#""\\1\\2A\\3"$in);
    
$in=preg_replace("#(^|$x)([Q])($x|$)#""\\1\\2U\\3"$in);
    
$in=preg_replace("#(^|$x)([J])($x|$)#""\\1\\2OTT\\3"$in);
    
$in=preg_replace("#(^|$x)([V])($x|$)#""\\1\\2AU\\3"$in);
    
$in=preg_replace("#(^|$x)([X])($x|$)#""\\1I\\2\\3"$in);
    
$in=preg_replace("#(^|$x)([Y])($x|$)#""\\1\\2PSILON\\3"$in);
    
$in=preg_replace("#(^|$x)([Z])($x|$)#""\\1\\2ETT\\3"$in);

    
$in=str_replace("&Auml;""Ä"$in);
    
$in=str_replace("&Ouml;""Ö"$in);
    
$in=str_replace("&Uuml;""Ü"$in);
    
$in=str_replace("&auml;""ä"$in);
    
$in=str_replace("&ouml;""ö"$in);
    
$in=str_replace("&uuml;""ü"$in);

    
$len=strlen($in);
    
$in.=" X";
    
$out="";
    for (
$a=0;$a<$len;$a++){
    
$c=$in[$a];
    if (
$c=="&"){
        for (
$b=$a+1;$b<$a+8;$b++){
        if (!
eregi("[a-z]"$in[$b])) break;
        }
        if (
$in[$b]==";"){
        for (;
$a<=$b;$a++) $out.=$in[$a];
        
$a--;
        } else 
$out.=$c;
    } else {
        if (
eregi("[aeiouäöüy]"$c)){
        
$next=$in[$a+1];
        if (
eregi("[aoeiouöüüy]"$next)) $nexton=true;
        else 
$nexton=false;
        
$both=strtolower($c.($nexton?$next:""));
        if (
strtoupper($c.$next)==$c.$next){
            
$uppers=true;
        } else {
            
$uppers=false;
        }
        if (
$both=="au" || $both=="ei" || $str=="eu" || $both=="äu"
        
|| $both=="öu"){
            
$out.="a";
        } else {
            if (
$c=="y"$out.="i";
            else if (
$c=="Y"$out.="Y";
            else 
$out.=$c;
            if (
$nexton){
            if (
$both!="ie"$out.=$next;
            }
        }
        if (
$uppers){
            
$out.="B";
        } else {
            
$out.="b";
            
$c=strtolower($c);
            
$next=strtolower($next);
        }
        
$out.=$c;
        if (
$nexton$out.=$next;
        if (
$nexton$a++;
        }else 
$out.=$c;
    } 
    }
    return 
$out;
}

function 
strtobsprachehtml($in$newurl=""$bbase=""){ 
    
setlocale(LC_ALL"de_DE");
    
$len=strlen($in);
    
$html=false;
    
$quoted=false;
    
$string=false;
    
$s="";
    
$out="";
    for (
$a=0;$a<$len;$a++){
    
$hon=false$hoff=false;
    if (!
$quoted){
        if (
$html && $in[$a]=="\""$string=!$string;
        else if (!
$string && $in[$a]=="<") { $html=true$hon=true; } 
        else if (!
$string && $in[$a]==">") { $html=false$hoff=true; }
        else if (
$string && $in[$a]=="\\"$quoted=true;
        else 
$quoted=false;
    } else {
        
$quoted=false;
    }
    if (
$hon){
        if (
$tagoff$out.=$s;
        else 
$out.=strtobsprache($s);
        
$s="<";
    } else if (
$hoff) {
            if (
$tagoff){
                if (
eregi("</".$tagoff."([[:space:]>]|$)"$s, &$erg)){
                    
$tagoff="";
                }
            }
            if (!
$tagoff && eregi("<(textarea|script|style)([[:space:]>]|$)"$s, &$erg)){
                
$tagoff=$erg[1];
            } else if (
eregi("<input.*[[:space:]]type=\"?submit\"?"$s)){
                if (
preg_match("#\s(value=)\"(.*[^\\\\])\"#Ui"$s, &$erg)){
                    
$s=str_replace($erg[1]."\"".$erg[2]."\""$erg[1]."\"".strtobsprache($erg[2])."\""$s);
                } else if (
preg_match("\s(value=)([^\s>]*)#Ui"$s, &$erg)){
                    
$s=str_replace($erg[1].$erg[2], $erg[1].strtobsprache($erg[2]), $s);
                }
            } else if (
eregi("<img[[:space:]]"$s)){
                if (
preg_match("#\s(alt=)\"(.*[^\\\\])\"#Ui"$s, &$erg)){
                    
$s=str_replace($erg[1]."\"".$erg[2]."\""$erg[1]."\"".strtobsprache($erg[2])."\""$s);
                } else if (
preg_match("#\s(alt=)([^\s>]*)#Ui"$s, &$erg)){
                    
$s=str_replace($erg[1].$erg[2], $erg[1].strtobsprache($erg[2]), $s);
                }
            } else if (
$newurl){
                if (
eregi("<a[[:space:]].*href="$s)){
                    if (
preg_match("#\s(href=)\"(.*[^\\\\])\"#Ui"$s, &$erg)){
                        
$s=str_replace($erg[1]."\"".$erg[2]."\""$erg[1]."\"$newurl?bsite=".urlencode($erg[2])."&bbase=".urlencode($bbase)."\""$s);
                    } else if (
preg_match("#\s(href=)([^\s>]*)#i"$s, &$erg)){
                        
$s=str_replace($erg[1].$erg[2], $erg[1]."\"$newurl?bsite=".urlencode($erg[2])."&bbase=".urlencode($bbase)."\"",$s);
                    }
                } else if (
eregi("<frame[[:space:]].*src="$s)){
                    if (
preg_match("#\s(src=)\"(.*[^\\\\])\"#Ui"$s, &$erg)){
                        
$s=str_replace($erg[1]."\"".$erg[2]."\""$erg[1]."\"$newurl?bsite=".urlencode($erg[2])."&bbase=".urlencode($bbase)."\""$s);
                    } else if (
preg_match("#\s(src=)([^\s>]*)#i"$s, &$erg)){
                        
$s=str_replace($erg[1].$erg[2], $erg[1]."\"$newurl?bsite=".urlencode($erg[2])."&bbase=".urlencode($bbase)."\"",$s);
                    }
                } else if (
eregi("<form[[:space:]].*action="$s)){
                    if (
preg_match("#\s(action=)\"(.*[^\\\\])\"#Ui"$s, &$erg)){
                        
$s=str_replace($erg[1]."\"".$erg[2]."\""$erg[1]."\"$newurl\""$s);
                        
$s.=">"."<input type=hidden name=bsite value=\"".$erg[2]."\">";
                        
$s.="<input type=hidden name=bbase value=\"$bbase\"";
                    } else if (
preg_match("#\s(action=)([^\s>]*)#Ui"$s, &$erg)){
                        
$s=str_replace($erg[1].$erg[2], $erg[1]."\"$newurl\""$s);
                        
$s.=">"."<input type=hidden name=bsite value=\"".$erg[2]."\">";
                        
$s.="<input type=hidden name=bbase value=\"$bbase\"";
                    }
                }
            }
        
$out.=$s;
        
$s=">";
    } else {
        
$s.=$in[$a];
    }
    }
    if (
strlen($s)>0){
    if (!
$html$out.=strtobsprache($s);
    else 
$out.=$s;
    }
    return 
$out;
}

function 
ob_b($in){ // Special for ob_start('ob_b') */
    
return strtobsprachehtml($in);
}
?>