I'm trying to create common masks from a string like so:
012abc.d+e_fg~hijk => 012{start}.d+{middle}_fg~{end}jk
replace:
$arrFromTo = array(
'st' => '{pre}',
'abc' => '{start}',
'e' => '{middle}',
'hi' => '{end}',
'dd' => '{post}'
);
Instead I keep overlapping replacements and get something like this instead (using a loop of str_replace's):
012{{pre}art}.d+{mi{post}le}_fg~{end}jk
Because the st is found in the already replaced {start} and dd is found in {middle}.
How would you replace the following?
$str = 'abc.d+e_fg~hijk';
echo replace_vars($str); // Desired output: 012{start}.d+{middle}_fg~{end}kJ
'dd' => '{post}'was in a different position, but that raised other issues./(?<!{\w*)(e)(?!\w*})/iThis would mean give me any "e" where it is not between curly braces and any other word character. The \w* makes it a variable-length lookahead/behind. Apparently pcre handles the variable-length lookahead well, but not the lookbehind.ncharacters that match the firstncharacters of something in your replace list. Once you have a full match, replace the lastncharacters with your replacement string.