0

I have a file that contains several colors as hexadecimals (i.e. #000 or #ffffff) and I'm trying to replace each of them with @varN where N is a number that increments with each replacement done. I believed my code does that, but $count always returns 196 after doing the placement even though I put the limit to 1 so count should never goes past 1. This results an endless loop. Why isn't it the limit working and what I can do to get the desired output?

$list = file($filepath.$file, FILE_USE_INCLUDE_PATH);
$pattern = "/#((([a-f]|[A-F]|[0-9]){6})|(([a-f]|[A-F]|[0-9]){3}))/";
$replaceVar = "@var";
$replaceNum = 0;
$count = 1;
while($count != 0){ //never ends
    $replacement = $replaceVar.$replaceNum;
    $output = preg_replace($pattern,$replacement,$list,1,$count);
    $replaceNum++;
    echo $replaceNum." ".$count."\n"; //returns $replaceNum and 196
}
file_put_contents($filepath.'/new'.$file,$output,FILE_USE_INCLUDE_PATH);

Example input file:

#000000 #111111 #123456 #abcdef #123 #abc

Example output file:

@var1 @var2 @var3 @var4 @var5 @var6

2
  • file put all contents to an array, you'd better use file_get_contents, here your $count wouldn't decrease so it's normal to have an infinite loop and what is $count? Commented Apr 29, 2014 at 19:00
  • file_get_contents gives me a string though, meaning I would have to some how turn the string back into a file with all the newlines in the right spot. I understand that $count is the number of times a replacement has been done to my $list. And with $limit = 1, $count should never go above 1. Commented Apr 29, 2014 at 19:15

2 Answers 2

1

You don't use the good function to do that for several reasons, I suggest you to use preg_replace_callback instead. Example:

$source = file_get_contents($yourfile);

$pattern = '~#[[:xdigit:]]{3}(?:[[:xdigit:]]{3})?~';
$count = 0;

$output = preg_replace_callback($pattern,
                       function ($m) use (&$count) { return '@var' . ++$count; },
                                $source);
Sign up to request clarification or add additional context in comments.

3 Comments

It works, however your regex gave me some unexpected results.
@user2361174: What kind of unexpected results?
@user2361174: Sorry my mistake! It is not [:alnum:] but [:xdigit:] for hex numbers!
1

You are always replacing on $list, but writing the result of the replacement to $output:

$output = preg_replace($pattern,$replacement,$list,1,$count);

This means, your $list will ALWAYS contain 1 match, when it starts with 1 - its never modified. Therefore your while will run without an end. You have to REPLACE it inside the same string you are scanning for more hits:

$list = preg_replace($pattern,$replacement,$list,1,$count);

now - at one point - $count should become zero, and $list contain no more unwanted matches.

1 Comment

The loop stopped, but it would always used @var0 as the replacement.

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.