Basically, I would like to create a PHP version of this sound changer. It is a tool used to take one language/lexicon, a set of sound changes (usually to mimic changes over a period of time, resulting in a new sub-language), apply said changes and show the results. The problem is that it is written in C, and my knowledge is in PHP. I would like to create something I have more knowledge in so I can improve it.

I realize it would be easier to start with "regular" regex but I would prefer to work with the special regex for this purpose. I've worked to try to convert the special regex into one I can use in PHP but, not having a lot of experience with regex, I'm not having much success. For one, I'm not sure I'm formatting the converted regex properly as preg_match matches some patterns but not others, and I'm not sure how to use it in preg_replace properly to replace only the letter(s) that need replacing, not the whole pattern. The letter is not always in the same position in the pattern so it's confusing me.

My script so far takes the set of rules (taken directly from the example on the page linked above, also explained on said page, basically x/y/z or find/replace/condition), and is trying to convert them into a workable regex. When I hope to get this working I hope to have a form to be able to input a different lexicon, alphabet and rules.

This is what I have so far:
http://demo.geekish.me/soundchanger/


PHP Code:
    $lexicon = array('lector','doctor','focus','jocus','districtus','civitatem','adoptare','opera','secundus');
    
$alphabet = array('V=aeiou','C=ptcqbdgmnlrhs','F=ie','B=ou','S=ptc','Z=bdg');
    
$rules = array('s//_#','m//_#','e//Vr_#','v//V_V','u/o/_#','gn/nh/_','c/i/F_t','c/u/B_t','p//V_t','ii/i/_','e//C_rV');
    
    
# converting alphabet array into wanted key => value
    
    
foreach($alphabet as $alpha) {
        
$a[] = explode("=",$alpha);
    }
    
$alphabet = array();
    foreach(
$a as $b => $c) {
        
$alphabet[$c[0]] = trim($c[1]);
    }
    
    
# rules
    
    
foreach($rules as $rule) {
        
$r[] = explode("/",$rule);
    }
    
$rules = array();
    foreach(
$r as $s => $t) {
        
$rules[] = array("find" => $t[0], "replace" => $t[1], "condition" => $t[2]);
    }
        
    
$find "";
    
$replace "";
    foreach(
$rules as $rule) {
        
        
$find trim($rule['find']);
        
printR("FIND: $find");
        
$replace trim($rule['replace']);
        
printR("REPLACE: $replace");
        
$condition trim($rule['condition']);
        
printR("CONDITION: $condition");
        
        
# replace # with $ to indicate end of line
        
$condition preg_replace('([a-z])','($0)',$condition);
        
$condition str_replace("_#","($find)\$",$condition);
        
$condition str_replace("#_","^($find)",$condition);
        
$condition str_replace("_","($find)",$condition);
        foreach(
$alphabet as $key => $alpha) {
        
//    $find = str_replace($key,"([$alpha])",$find);
        //    $replace = str_replace($key,"([$alpha])",$replace);
            
$condition str_replace($key,"([$alpha])",$condition);
        }
        
        
$condition "/$condition/";
                        
        
printR("REGEX: $condition");
        
        
        
        foreach(
$lexicon as $key => $word) {
            
$match preg_match($condition,$word,$matches);
            if(
count($matches)) {
                
printR("\n\n");
                
printR($word);
                
printR($matches);
        
//        $word = preg_replace($condition,$replace,$word);
        //        printR($word);
            
}
        
//    $lexicon[$key] = $word;
        
}
        
        
printR("\n\n\n");
        
    } 
Commented out the stuff that I haven't used yet or that isn't working. printR is the only custom function, and all it is is a quick modification of print_r that wraps the printed text in <pre> tags.

Any ideas? Can anyone point me in the right direction or to a different way to do this? Am I trying to do the impossible?