0

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!

2
  • Now is it real simple, if you have that array what you shown. Iterate through an all rows, iterate through on all columns, and with a 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, like myfile.csv.old, and rename your temp file to the orig filename. Commented Oct 25, 2016 at 9:53
  • Start by specifying the correct delimiter in your str_getcsv call. Commented Oct 25, 2016 at 10:11

1 Answer 1

0

You could parse the csv data by specifying a different delimiter (space instead of comma) but for this particular operation that isn't even necessary. Just search+replace the particular word in-place:

$csv = file_get_contents("your file with spaces.csv");
$search = "Active";
$replace = "Passive";
$csv = preg_replace("/(^|\\s)$search(\\s|$)/si","\\1$replace\\2",$csv);
file_put_contents("modified.csv",$csv);

Note that the funky regex is to avoid replacing Active if it's part of a word. For example, if there is a word like Actively or Interactive somewhere in your data you probably don't want to replace that. If you do, you can simply use $csv = str_replace('Active','Passive',$csv);

If you want a case sensitive replace, e.g. replace Active but not active, remove the i modifier at the end of the /si regex flags.

Sign up to request clarification or add additional context in comments.

3 Comments

what I'm worried about is do I have to make sure that when I write again on the .csv file the lines must match the lines from the file before the modifications? I really like your suggestion above, this is the only thing that bothers me.
The code above will not change the structure of the file. Regardless of whether it's csv or any other format. It will only change words into other words. Note that if you would also like to use it for .csv files that are actually comma separated (i.e. if they really use commas, no spaces) then you'd have to change the regex into: "/(^|\\s|,)$search(\\s|,|$)/si"
Thank you so much for the explanation, it really worked great!

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.