0

I'm looking for a way to convert a numeric into a string that could be used to represent a version.

Let's say we have

$foo = 123

I'd like to be able to convert this into $foo = "1.2.3"

So far I've tried

$foo = 123;
$string = preg_replace("[^0-9]",'$0\.',$foo);

But that doesn't seem do do anything at all, $string just comes back as empty/null.

What am I doing wrong?

4

4 回答 4

2

你可以这样做,(文档):

$string = wordwrap($foo, 1, '.', true); 
于 2013-05-16T08:48:32.203 回答
0

你说它已经是数字了。

$foo = 123;
echo implode('.', str_split($foo));
于 2013-05-16T08:50:42.610 回答
0

^ 表示 NOT 即 [^0-9] 表示不在 0123456789 中的任何内容

您还需要使用 / 分隔搜索(其他字符可用)
这允许您使用修饰符(您可以查找)

您不需要转义替换字符,因为它不是 REGEX 模式

$foo = 123;
$string = preg_replace("/[0-9]/",'$0.',$foo);

你看过preg_replace()php手册中的例子吗?
http://php.net/manual/en/function.preg-replace.php

于 2013-05-16T08:50:53.690 回答
0

只需使用str_split(), 后跟join():

join('.', str_split($foo));
于 2013-05-16T08:50:05.950 回答