In un file testuale utilizzo le seguenti istruzioni per sostituire in un file (con estensione TXT) il contenuto dei campi di un database, i primi quattro parametri vengono sostituiti correttamente, mentre il quinto non viene sostituito. Come risolvo?

$sql_row_number = mysqli_query($link,"SELECT COUNT(*) FROM `TBL_NAME`");
$row_number
= mysqli_fetch_array($sql_row_number);
for($i =1; $i <= $row_number[0]; $i++){
$sql
= mysqli_query($link,"SELECT * FROM `TBL_NAME` WHERE `id`='$i' AND `ttttttt`='BIANCO' AND `ppppppp`='Non pagata'")ordie("Errore query database: ". mysqli_error($link));
while($value = mysqli_fetch_array($sql)){
copy
("/path/file.txt","/Applications/XAMPP/xamppfiles/htdocs/Recupero_crediti/Lettera recupero crediti DEA codici bianchi_$i.txt");
$complete_path
="/path/file_$i.txt";
if($value['Cognome e Nome']!= NULL){
$file_content1
= file_get_contents($complete_path);
file_put_contents
($complete_path, str_replace("%COGNOME_NOME%", $value['Cognome e Nome'], $file_content1));
}else{
file_put_contents
($complete_path, str_replace("%COGNOME_NOME%","", $file_content1));
}
if($value['Indirizzo']!= NULL){
$file_content2
= file_get_contents($complete_path);
file_put_contents
($complete_path, str_replace("%INDIRIZZO%", $valeu['Indirizzo'], $file_content2));
}else{
file_put_contents
($complete_path, str_replace("%INDIRIZZO%","", $file_content2));
}
if($value['CAP']!= NULL){
$file_content3
= file_get_contents($complete_path);
file_put_contents
($complete_path, str_replace("%CAP%", $value['CAP'], $file_content3));
}else{
file_put_contents
($complete_path, str_replace("%CAP%","", $file_content3));
}
if($value['Comune di Residenza']!= NULL){
$file_content4
= file_get_contents($complete_path);
file_put_contents
($complete_path, str_replace("%COMUNE_DI_R.%", $value['Comune di Residenza'], $file_content4));
}else{
file_put_contents
($complete_path, str_replace("%COMUNE_DI_R.%","", $file_content4));
}
if($value['Data']!= NULL){
$file_content5
= file_get_contents($complete_path);
file_put_contents
($complete_path, str_replace("%DATA%", $value['Data'], $file_content5));
}else{
file_put_contents
($complete_path, str_replace("%DATA%","", $file_content5));
}
//unlink($complete_path);
}
}Ps Se scrivo %DATA% al posto di, ad esempio, %CAP% la sostituzione avviene correttamente anche per il quinto parametro, come se dipendesse dalla loro posizione...