Well - it seems that the reg. exp handler is doing exactly what you asked it to do. It is replacing all occurances of "i" with "phwoot", so the first one is at the end of "Hi" -> "Hphwoot".
What you need to do is be more specific in your regular expressions, this is a bit more work - but you have to tell it exactly what it should be doing.
The first idea, to put spaces around the items in $patterns and $replacements would seem to work initially with the example you provide: -
<?php
$patterns[0] = "/ the /";
$patterns[1] = "/ i /";
$patterns[2] = "/ walk /";
$replacements[0] = " lung ";
$replacements[1] = " phwoot ";
$replacements[2] = " chow ";
ksort($patterns);
ksort($replacements);
echo("$string translated is: ");
print preg_replace($patterns, $replacements, $_POST["string"] );
?>
- prints out "Hi lung walk" - but wouldn't match values where the words were the first or last characters (since there would be no space before/after the string to be matched.
To cut a long story short there are 2 potential solutions: -
1, pad out your $_POST["string"] with a space on the beginning and end before passing to preg_replace, then remove them from the pregged string afterwards. This is a quick hack
2, revisit your reg-exp and account for the beginning of string and end of string as new conditions - this one I leave for you to investigate further 🙂