I have a function using js replace and having hard time converting it to php
The JS code (both does the same thing see js fiddle links)
function number_to_code(s) {
// converting the value to something like 50000.00 to look like currency
// next replace it with the codes as bellow.
s = parseFloat(s).toFixed(2).replace(/\d/g, m => ({
'0': 'I',
'1': 'N',
'2': 'E',
'3': 'S',
'4': 'T',
'5': 'O',
'6': 'M',
'7': 'A',
'8': 'L',
'9': 'D'
})[m]);
// grouping it with ,
s = s
.replace(/([A-Z])(\1+)/g, (_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
})
.replace(/\B(?=(\w{3})+(?!\w))/g, ",")
return s;
}
they convert the currency value to a text code.
I think php preg_replace is the function to use. I can't understand the
(_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
part