我的目标是获得类似的内容:150.000,54
或48.876,05
这意味着我的逗号是十进制启动器。
到目前为止,这是我的代码:
<?php
//cut numbers after comma if there are any, after 2 digits
$matchPattern = '/[0-9]+(?:\,[0-9]{2}){0,2}/';
//remove everything except numbers, commas and dots
$repl1 = preg_replace("/[^a-zA-Z0-9,.]/", "", $input);
//let there be a 0 before comma to have values like 0,75
$repl2 = preg_replace("/^[0]{1}$/", "",$repl1);
//now i need you here to help me for the expression putting dots after each 3 numbers, until the comma:
$repl3 = preg_replace("/regexphere$/", ".", $repl2);
preg_match($matchPattern, $repl3, $matches);
echo($matches[0]);
?>
我知道preg_replacing 3次是愚蠢但我不擅长写正则表达式。如果你有更好的想法,不要分享它,但也要解释。我知道一些类型:
http://regexone.com/lesson/0
提前谢谢。
-------- -------- UPDATE
因此,我需要处理0000,45
类似于0,45
的输入,并010101,84
输入到1,84
完成后,我就完成了。
$input = Input::get('userinput');
$repl1 = preg_replace("/[^0-9,.]/", "", $input);
$repl2 = preg_replace("/^0/", "",$repl1);
$repl3 = str_replace(".","",$repl2);
preg_match('/[0-9]+(?:\,[0-9]{2}){0,2}/', $repl3, $matches);
$repl4 = preg_replace('/(\d)(?=(\d{3})+(?!\d))/', '$1.', $matches[0]);
return repl4;
---- ---- UPDATE
这是我到目前为止所得到的:https://ideone.com/5qmslB
我只需要在数字之前删除逗号之前的零。
答案 0 :(得分:2)
我不确定这是最好的方法,但我希望它有用。
以下是我与假$input
:
<?php
$input = "textmdwrhfejhg../,2222233333,34erw.re.ty";
//cut numbers after comma if there are any, after 2 digits
$matchPattern = '/[0-9]+(?:\,[0-9]{2}){0,2}/';
//remove everything except numbers, commas and dots
$repl1 = trim(preg_replace("/[^0-9,.]/", "", $input), ".,");
echo "$repl1" . "\n";
//let there be a 0 before comma to have values like 0,75, remove the 0
$repl2 = preg_replace("/^0/", "",$repl1);
echo "$repl2" . "\n";
//The expression putting dots after each 3 numbers, until the comma:
$repl3 = preg_replace('/(\d)(?=(?:\d{3})+(?!\d))/', '$1.', $repl2);
echo "$repl3" . "\n";
表达式在每3个数字之后放置点是
(\d)(?=(?:\d{3})+(?!\d))
在这里,你可以see how it works。在简单的人类中,
(\d)
- 我们将在替换模式中使用的捕获组,匹配单个数字.... (?=(?:\d{3})+(?!\d))
- 后跟3位数组。外部(?=...)
是一种前瞻性构造,可以检查但不消耗字符,(?:\d{3})+
是一个非捕获组(不需要将匹配的文本保留在内存中),它们完全匹配3位数(由于限制量词{3}
)1次或多次(由于+
量词),(?!\d)
是否定前瞻检查最后一个匹配的3位数组之后的下一个字符不是数字。如果小数点分隔符后面的位数超过3位,则不起作用。使用正则表达式,我只能想到一种方法,用(?<!,)(\d)(?=(?:\d{3})+(?!\d))
支持十进制后的4位数。不确定在PHP中是否存在没有可变宽度后视的通用方法(在这里,我们也需要一个可变宽度的前瞻)。因此,您可以考虑用逗号分割$repl2
值,并仅将第一部分传递给正则表达式。然后,结合。像这样:
$spl = split(',', $repl2); // $repl2 is 1234,123456
$repl3 = preg_replace('/(\d)(?=(?:\d{3})+(?!\d))/', '$1.', $spl[0]);
$repl3 .= "," . $spl[1]; // "1.234" + "," + "123456"
echo "$repl3" . "\n"; // 1.234,123456
<强>更新强>:
final code I have come up with:
$input = "textmdwrhfejhg../0005456,2222233333,34erw.re.ty";
//Here's mine :
$repl1 = trim(preg_replace("/[^0-9,.]/", "", $input), '.,');
//following line just removes one zero, i want it to remove all chars like
//Input : 000549,569 Output : 549,569
echo "$repl1\n";
$repl2 = preg_replace("/^0+(?!,)/", "",$repl1);
$repl3 = str_replace(".","",$repl2);
preg_match('/[0-9]+(?:\,[0-9]{2}){0,2}/', $repl3, $matches);
$repl4 = preg_replace('/(\d)(?=(\d{3})+(?!\d))/', '$1.', $matches[0]);
echo $repl4;