2

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

0

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

both works perfect, thank you, first one is what im looking for
@salathe If you set the 4th param. to -1 it could make several replacements at one loop step. If you set a upper-'S' at the end of your regular expression it will be optimized and is much faster.
1

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
}

2 Comments

It should work but is never ending too. ive tried, i know what are u trying and is good the idea, but it dont work.
i know why the loop is never ending, if you dont save the return value from the regex in one variable, the variable named string never changes and always be the same comparation. but if u save it in to a variable u will compare $string as $string and the loop will stop at the first iteration.
1

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...

1 Comment

thanks for the answer but the loop stop at the first ocurrence, when i put $string=preg_replace('/(\d)((\d\d\d)+\b)/','$1,$2',$string will be add a coma every time its matched, when i put repeatedly works, but when i put in a loop dont work. i dont know if u undersnatnd me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.