-1

I have a file that looks like this:

id,filename,lastname     
1234,aron,shushu     
1221,moshe,sis

And I wanted to for example change the name of id "1221" to "minim", Anyone know how to do this in PHP?

I tried to split it with explode like this:

$str1 = explode("\n",$str);    
$str2 = explode(",",$str1);

And I just did not succeed I just can not come up with the right solution I was looking for a lot

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 3
    Welcome to Stack Overflow. While we're glad to help when you're stuck, you are still expected to make an effort of your own towards reaching the solution. If you have tried something, please share your code and explain where it fails to do what you need. – El_Vanja Feb 08 '21 at 13:26
  • I added in the post – אהרן שובקס Feb 08 '21 at 13:41
  • `did not succeed` doesn't tell us what went wrong. Tell us about any errors you get, and/or any unexpected output. P.S. There are ready-made libraries in PHP for reading and writing CSV files. There's not really any reason to start writing your own. Don't re-invent the wheel. – ADyson Feb 08 '21 at 13:42
  • 1
    `$str2 = explode(",",$str1);` won't work because `$str1` isn't a string, it's an array. Either way, you could find a library that deals with a specific file type like @ADyson suggested or if this is just some regular text file, have a look at [this question](https://stackoverflow.com/questions/3004041/how-to-replace-a-particular-line-in-a-text-file-using-php). – El_Vanja Feb 08 '21 at 13:43
  • 1
    Thanks for the answer but it's not a csv file it's a file that looks like csv Failed it means I could not get the correct update code I tried to use with str_replace but it changed me in all places for example I wanted to replace 1 for 2 so it replaced me every place written 1 I want to do as there is in sql update – אהרן שובקס Feb 08 '21 at 13:48
  • `it's a file that looks like csv `.and what exactly is the difference then? Are you saying the values are not separated by commas? Because in the example you gave, they clearly are. If you simply mean that it doesn't have a .CSV extension on the filename, that's irrelevant. If the content is CSV then it can be processed as a CSV. If it looks like a dog, barks like a dog, but has a sign saying "cat" round its neck, then...it's probably still a dog. – ADyson Feb 08 '21 at 13:56
  • `Failed it means I could not get the correct update code`...well obviously, otherwise you wouldn't be here. But you need to tell us _how_ it failed, e.g. error messages, unexpected results etc. And give proper examples of the actual code you used, not just a vague description of it. Edit your question to include proper details - don't bury the information in these comments. Or...just use a CSV-parsing library that can already do most of the work, as I already suggested. – ADyson Feb 08 '21 at 14:01
  • 1
    Or better still...store the data as JSON rather than CSV, then it's much easier to find individual pieces of data in it. CSV is designed for one-off bulk data transfer, not as a data repository for reading and writing individual items. Or better still than that...just use a database like everyone else. If you don't want to have a full database server, you can use a file-based database such as SQLite, which requires no additional setup or maintenance, and is fine for small databases with small numbers of users. – ADyson Feb 08 '21 at 14:03

1 Answers1

1

This code should work for you. Place your content in file and run this code.

$file = fopen( "file.txt", "r" );
$line = 0;

while ( ! feof( $file ) ) {
    $lineToChange = fgets( $file );
    if ( $line > 0 && $line < 3) {
        $commaPosition = strpos( $lineToChange, ',' );
        $changedStr = substr_replace($lineToChange,'minim',0,$commaPosition);
        $result .= $changedStr;
    }
    else{
        $result .= $lineToChange;
    }
    $line ++;
}
fclose( $file );

file_put_contents('file.txt', $result);
echo $result;
rameezmeans
  • 830
  • 1
  • 10
  • 21