dqnek0079 2014-06-02 15:25
浏览 44
已采纳

格式数字字符串

I've the following string (meaning it's not numeric):

"0870490055012000000000"

wich is always 22 characters long. I need to transform into:

"087.049.0055.0120.0000.0000"

Using PHP or even on js/client side.

I found something like this but was not able to solve the problem.


I guess there are many ways to solve this. I'm just asking for something like:

$x= format("00000", "xxx.xxx..xxx.x.x.x")

or

$x = preg_replace("/a;;w.;w;e;ew")
  • 写回答

1条回答 默认 最新

  • dp411805872 2014-06-02 15:34
    关注

    with PHP, you can use this:

    $str = preg_replace('~\A\d{3}\K\d{3}|\d{4}~', '.$0', $str);
    

    where \A is an anchor for the start of the string.

    \K removes all that have been matched on the left from match result.

    If you need something more general to apply a mask to a string, the link you shared in your question will give you the way to do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?