I have a .csv file in which I need to replace some words using php (for example, the word Active). However, the values in the file seem to be separated by spaces, not comas and I'm not sure what is the right way to read from the file, make the changes and then write back to the file in order not to damage its structure and organization (if this may be a problem).
This is what the content by rows looks like when shown using
str_getcsv();
[1]=> array(1) { [0]=> string(193) "Format Version 4 " }
[2]=> array(1) { [0]=> string(359) "Campaign Active KNCTR 20 DailyBudgetAccelerated HelsinkiKyivRigaSofiaTallinnVilnius 2016-10-11 11:05:43Z " }
[3]=> array(1) { [0]=> string(325) "Campaign Location Target Active Active KNCTR AU PeopleIn 0 Australia 2016-10-06 14:25:13Z " }
[4]=> array(1) { [0]=> string(319) "Campaign Location Target Active Active KNCTR CA PeopleIn 0 Canada 2016-10-06 14:25:13Z " }
If I get the content of the file using
file_get_contents();
again there are no comas.
So, how would you suggest I make the changes so that I only replace the needed words, not damage the document?
I have no experience with this kind of files, so my question may need some clarifying. Thank you!
preg_replace, replace your word. Create a tmp csv file, put the new values into it, when you finished, rename the original file to something, likemyfile.csv.old, and rename your temp file to the orig filename.