php分割大数(如爆炸)

发布于 2024-10-18 03:19:25 字数 131 浏览 4 评论 0原文

我需要 phpexplode() 的功能,但没有分隔符。

例如,将变量“12345”变成一个数组,分别保存每个数字。

这可能吗?我已经用谷歌搜索过,但只找到explode(),这似乎不起作用。

谢谢!

i need the functionality of php explode(), but without the separators.

for example, turning the variable "12345" into an array, holding each number seperately.

is this possible? i've already googled but only found explode(), which doesn't seem to work.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰つ倒转 2024-10-25 03:19:25

php 中的任何字符串:

$foo="12345";
echo $foo[0];//1
echo $foo[1];//2
//etc

或(来自 preg_split())手册中的页面

$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

更好:

$str = 'string';
$chars=str_split($str, 1)
print_r($chars);

的基准:

 function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}


$str = '12345';
$time_start = microtime_float();
for ($i = 0; $i <100000; $i++) {
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
//$chars=str_split($str, 1);
}
$time_end = microtime_float();
$time = $time_end - $time_start;

echo "$time seconds\n";

preg_split() 与 str_split()结果

str_split  =0.69
preg_split =0.9

with any string in php:

$foo="12345";
echo $foo[0];//1
echo $foo[1];//2
//etc

or (from the preg_split()) page in the manual

$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

EVEN BETTER:

$str = 'string';
$chars=str_split($str, 1)
print_r($chars);

benchmark of preg_split() vs str_split()

 function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}


$str = '12345';
$time_start = microtime_float();
for ($i = 0; $i <100000; $i++) {
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
//$chars=str_split($str, 1);
}
$time_end = microtime_float();
$time = $time_end - $time_start;

echo "$time seconds\n";

results:

str_split  =0.69
preg_split =0.9
反目相谮 2024-10-25 03:19:25

如果你真的想创建一个数组,那么使用 str_split(),即,

echo '<pre>'. print_r(str_split("123456", 1), true) .'</pre>'; 

会导致

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

If you actually want to create an array, then use str_split(), i.e.,

echo '<pre>'. print_r(str_split("123456", 1), true) .'</pre>'; 

would result in

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
灰色世界里的红玫瑰 2024-10-25 03:19:25

您的号码可以转换为字符串,然后像数组一样运行

$i = 2342355; $i=(string)$i;
//or
$i='234523452435234523452452452';

//then

$i[2]==4

//numeration started from 0

Your number can be turned into string and then acted like an array

$i = 2342355; $i=(string)$i;
//or
$i='234523452435234523452452452';

//then

$i[2]==4

//numeration started from 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文