如何格式化会计代码

时间:2015-10-27 08:42:53

标签: javascript php jquery

我想用12345678的php显示会计帐号代码,形成123.45.678,有什么人可以帮帮我吗?谢谢

2 个答案:

答案 0 :(得分:0)

你可以在php代码中使用preg_replace(对于你的例子中的8个字符数字)使用正则表达式

$number = 12345678;

echo "Number is: ${number}";
$new_number=preg_replace('/(\d{3})(\d{2})(\d{3})/','$1.$2.$3', $number);

echo "Number is: ${new_number}";

答案 1 :(得分:0)

我快速写了这篇文章,它似乎提供了你想要的功能:

https://jsfiddle.net/tdaeunem/

function splitNumber(inputNumber){

    var firstPart = /^(\d{3})/;
    var secondPart = /^\d{3}(\d{2})/;
    var thirdPart = /^\d{5}(\d{3})/;

    var matches = [];

    matches[0] = inputNumber.match(firstPart)[1];

    matches[1] = inputNumber.match(secondPart)[1];

    matches[2] = inputNumber.match(thirdPart)[1];

    return matches.join(".");

}

console.log(splitNumber("12345678"));