将 JavaScript 正则表达式函数转换为 php
Convert a JavaScript regexp function to a php
我有一个使用 js 替换的函数,但很难将其转换为 php
JS代码(两者作用相同见js fiddle链接)
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])(+)/g, (_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
})
.replace(/\B(?=(\w{3})+(?!\w))/g, ",")
return s;
}
他们将货币值转换为文本代码。
我觉得phppreg_replace是要用的函数。
我无法理解
(_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
部分
您可以使用 php 函数做同样的事情 preg_replace_callback 唯一的区别是 preg_replace_callback 函数只有一个参数,它是一组匹配项。
例如:
$string = "1 1";
preg_replace_callback('/(\d) (\d)', function($matches) {
// The first position of the $matches is the whole string
// The second position is the first group and so on...
// And then you can return whatever you want, using or not the groups from the matches
}, $string);
啊,在@WiktorStribiżew 的帮助下,我设法获得了最终输出。
对于正在寻找有关 php preg_replace() 的优秀教程的任何人
$string = "500000120034000567080900"; //test string
$a = array("0"=>"I", "1"=>"N", "2"=>"E","3"=>"S","4"=>"T","5"=>"O","6"=>"M","7"=>"O","8"=>"L","9"=>"D");
$pattern = '/(\d)/';
$pattern2 = '/II+/';
// the function call
$result = preg_replace_callback($pattern,
function($matches) {
// echo ($matches[1]." ");
global $a;
return $a[$matches[1]];
},$string);
$result2 = preg_replace_callback($pattern2,
// the callback function
function($matches) {
return substr_replace(str_replace("I","Z",$matches[0]),"I",0,1);
//return $a[$matches[0]];
},$result);
echo "<br/>".$result;
echo "<br/>".$result2;
OIIIIINEIISTIIIOMOILIDII <= intermediate step to help understand.
OIZZZZNEIZSTIZZOMOILIDIZ <= this is the output I was looking for
我有一个使用 js 替换的函数,但很难将其转换为 php
JS代码(两者作用相同见js fiddle链接)
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])(+)/g, (_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
})
.replace(/\B(?=(\w{3})+(?!\w))/g, ",")
return s;
}
他们将货币值转换为文本代码。
我觉得phppreg_replace是要用的函数。 我无法理解
(_, g1, g2) => {
return g1 + g2.replace(/./g, "Z")
部分
您可以使用 php 函数做同样的事情 preg_replace_callback 唯一的区别是 preg_replace_callback 函数只有一个参数,它是一组匹配项。
例如:
$string = "1 1";
preg_replace_callback('/(\d) (\d)', function($matches) {
// The first position of the $matches is the whole string
// The second position is the first group and so on...
// And then you can return whatever you want, using or not the groups from the matches
}, $string);
啊,在@WiktorStribiżew 的帮助下,我设法获得了最终输出。
对于正在寻找有关 php preg_replace() 的优秀教程的任何人
$string = "500000120034000567080900"; //test string
$a = array("0"=>"I", "1"=>"N", "2"=>"E","3"=>"S","4"=>"T","5"=>"O","6"=>"M","7"=>"O","8"=>"L","9"=>"D");
$pattern = '/(\d)/';
$pattern2 = '/II+/';
// the function call
$result = preg_replace_callback($pattern,
function($matches) {
// echo ($matches[1]." ");
global $a;
return $a[$matches[1]];
},$string);
$result2 = preg_replace_callback($pattern2,
// the callback function
function($matches) {
return substr_replace(str_replace("I","Z",$matches[0]),"I",0,1);
//return $a[$matches[0]];
},$result);
echo "<br/>".$result;
echo "<br/>".$result2;
OIIIIINEIISTIIIOMOILIDII <= intermediate step to help understand.
OIZZZZNEIZSTIZZOMOILIDIZ <= this is the output I was looking for