while 循环内的 preg_replace

发布于 2024-11-16 00:48:38 字数 692 浏览 3 评论 0原文

为什么这个例子不起作用:

$string='1000000000000';

while($string=preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string)){}
echo $string."<br>";

它假设重复循环直到不再匹配。 直到正则表达式失败。但不,循环永远不会结束。

这个替代方案有效:

$string='10000000000000';

while(preg_match("/(\d)((\d\d\d)+\b)/",$string)){
    $string=preg_replace("/(\d)((\d\d\d)+\b)/","$1,$2",$string);
    }
    echo $string."<br>";

我的问题是: 当正则表达式仍然无法匹配时, preg_replace 函数返回一些 TRUE/FALSE 值? 如果返回 False,为什么在第一个示例中循环永远不会停止。 我尝试过:

while((regex)!==FALSE)
while((regex)==TRUE)

但它不起作用。

我不在乎如何放置逗号, 我想了解 preg_replace 函数

如果有人可以帮助我。 会很棒的。 谢谢

Why this example wont work:

$string='1000000000000';

while($string=preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string)){}
echo $string."<br>";

It supposed that the loop is repeated untill is not more to match.
Untill the regex fail. But no, the loop is never ending.

this alternative works:

$string='10000000000000';

while(preg_match("/(\d)((\d\d\d)+\b)/",$string)){
    $string=preg_replace("/(\d)((\d\d\d)+\b)/","$1,$2",$string);
    }
    echo $string."<br>";

My question is:
the preg_replace function returns some TRUE/FALSE value when the regex can't still matching?
If return False why in the first example the loop never stop.
Ive tried with:

while((regex)!==FALSE)
while((regex)==TRUE)

and it dont work.

I dont care about the how to put commas,
i wanna know about the preg_replace function

If someone can help me.
would be great.
thanks

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

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

发布评论

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

评论(3

一绘本一梦想 2024-11-23 00:48:38

preg_replace() 的可选第五个参数 $count 应用于跟踪进行了多少次替换。

你的循环可能看起来像

do {
    $string = preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2', $string, 1, $count);
} while ($count);

或者,或者,这个正则表达式将一步完成工作

 $string = preg_replace('/(?!^)(?=(?>\d{3})+$)/', ',', $string);

The optional 5th parameter to preg_replace(), $count, should be used to track how many replacements were made.

Your loop could look like

do {
    $string = preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2', $string, 1, $count);
} while ($count);

Or, alternatively, this regex will do the work in one step

 $string = preg_replace('/(?!^)(?=(?>\d{3})+$)/', ',', $string);
找回味觉 2024-11-23 00:48:38

来自手册

如果找到匹配,则将返回新的主题,否则将原样返回主题,如果发生错误,则返回 NULL。

因此,只要没有发生错误,您的赋值将始终评估为 true(字符串评估为 true)。

您可以做的是将返回值与主题进行比较,如果它们相同,则未找到匹配项:

while ($string != preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string))
{
  // a match was found
}

From the manual:

If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.

So as long as no error occurs, your assignment will always evaluate as true (a string evaluates as true).

What you could do is compare the return value to the subject, if they are the same, no match was found:

while ($string != preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string))
{
  // a match was found
}
無心 2024-11-23 00:48:38

如果找到匹配,则返回新的主题,否则主题将原样返回,如果发生错误,则返回 NULL。

http://php.net/manual/en/function.preg-replace.php

所以,代码需要是:

while(($string=preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string)) !== $string){}

...我认为...甚至不确定这是否是有效的 PHP...

If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.

http://php.net/manual/en/function.preg-replace.php

So, the code would need to be:

while(($string=preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string)) !== $string){}

...I think... not even sure if that's valid PHP...

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