Currently what you are doing is: sprintf("S%02dE%02d", 2, 05)
which is using octal. You're getting octal results because you need to treat the numbers with leading zeros as strings before you parse them rather than execute them directly. Thus the easy solution which you were seeking is simply to quote them like so:
$text = preg_replace('/([0-9]{1,2})x([0-9]{2})/e',
'sprintf("S%02dE%02d", "$1", "$2")', $text);
Alternatively, since the e
flag is depreciated, try using preg_replace_callback which is easier to work with:
$text = preg_replace_callback('/([0-9]{1,2})[x]([0-9]{2})/',
function($matches){return sprintf("S%02dE%02d", $matches[1], $matches[2]);}
, $text);