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
fileput all contents to an array, you'd better usefile_get_contents, here your$countwouldn't decrease so it's normal to have an infinite loop and what is$count?file_get_contentsgives 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$countis the number of times a replacement has been done to my$list. And with$limit = 1,$countshould never go above 1.