It's a shame the code is open-source but extremely unreadable...
I'll use those two as an example. Moving away from /e goes basically like:
1. Switch the function to preg_replace_callback and drop the /e flag, obviously.
2. Change the second argument (a string) to be a function following the template
function($matches) {
return ___;
}
where you put in there the replacement string but minus the outer quotes.
3. Replace "\\1" with $matches[1], "\\2" with $matches[2], and so on, and correct any issues with the PHP syntax.
So this
preg_replace("/\+([\x01-\x7F])#/e", "chr(ord('\\1') + 127)", $V341be97d)
starts as
preg_replace_callback("/\+([\x01-\x7F])#/", function($matches) {
return chr(ord('\\1') + 127);
}, $V341be97d)
and you replace the '\\1' string to get
preg_replace_callback("/\+([\x01-\x7F])#/", function($matches) {
return chr(ord($matches[1]) + 127);
}, $V341be97d)
What you have now is quite a bit different to the point that it won't actually work correctly.
If you're using PHP 7.4 or later, you can simplify the function syntax if you want by using a function shorthand: function(...) { return ... } can be shortened to fn(...) => ...
preg_replace_callback("/\+([\x01-\x7F])#/", fn($matches) => chr(ord($matches[1]) + 127), $V341be97d)
The second one
preg_replace("/#(#|[0-9a-f]{2})/ie", "'\\1' == '#' ? '#' : chr(hexdec('\\1'))", $V341be97d)
is similar
preg_replace_callback("/#(#|[0-9a-f]{2})/i", function($matches) {
return '\\1' == '#' ? '#' : chr(hexdec('\\1'));
}, $V341be97d)
but has a little more to replace, creating
preg_replace_callback("/#(#|[0-9a-f]{2})/i", function($matches) {
return $matches[1] == '#' ? '#' : chr(hexdec($matches[1]));
}, $V341be97d)
or with the PHP 7.4 shorthand,
preg_replace_callback("/#(#|[0-9a-f]{2})/i", fn($matches) => $matches[1] == '#' ? '#' : chr(hexdec($matches[1])), $V341be97d)
The latest one you're working on
preg_replace('/\^([^\^<])/e', "'`' . \$V70dda5df[ord('\\1') % 8]", $V341be97d)
goes very much the same way
preg_replace_callback('/\^([^\^<])/', function($matches) {
return '`' . \$V70dda5df[ord('\\1') % 8];
}, $V341be97d)
preg_replace_callback('/\^([^\^<])/', function($matches) {
return '`' . \$V70dda5df[ord($matches[1]) % 8];
}, $V341be97d)
except it has a wrinkle: it tries to use the $V70dda5df variable. You need to adjust the syntax a bit by removing the backslash (it was needed because the code lived inside a string) but also for the fact that variables aren't accessible inside functions... unless you explicitly tell PHP you want that with a "use" attached to the function.
That results in,
preg_replace_callback('/\^([^\^<])/', function($matches) use ($V70dda5df) {
return '`' . $V70dda5df[ord($matches[1]) % 8];
}, $V341be97d)
The thing about variables doesn't apply to shorthand functions, so with PHP 7.4 you could simply write
preg_replace_callback('/\^([^\^<])/', fn($matches) => '`' . $V70dda5df[ord($matches[1]) % 8], $V341be97d)