replace line in a text file php

Posted on 2008-10-17
Last Modified: 2013-12-13
I'm using fgetcsv to import data into mysql but the csv is corrupt and I had to find a way to fix it.

The attached code is what I'm using to fix the data but when I correct the line,    $new_line.  I need to replace the old line in the text file.  alternatively I could create a new file and write out each line as it is processed.  either way would work for me.

How do I write out the new text file?

$handle = fopen("Details.txt", "r");

if ($handle) {

    while (!feof($handle)) {


	    $haystack = fgets($handle, 4096);


				$i = 0;


				//Find the position of needle

					$position = 0;

					while ($i <= 13){

					$position=strpos($haystack,$needle, $position+1);

					//echo $position . "<br>";

					$i = $i + 1;


				// trim out the string before the first occurance of the needle.


				echo "start = " . $temp_string . '<br>';

				//find 14th comma

			  $endposition=strpos($haystack,$needle, $position+1);

			  //echo $endposition;

			  //get description1

		      $description1=substr($haystack,$position+2,$endposition - ($position + 3));

              $description1 = str_replace('"', '""', $description1);

              echo "description1=" . $description1 . "<br>";



	   $des2end=strpos($haystack,$needle,$endposition + 1);

	   //echo $des2end;

	   $description2=substr($haystack,$endposition + 2,$des2end - ($endposition+3));

	   $description2 = str_replace('"', '""', $description2);

	   echo "description2=" . $description2 . "<br>";


	   $rest =substr($haystack,$des2end);

	   echo "end = " . $rest . '<br><br>';

///this is the corrected line

	   $new_line = $temp_string . '"' . $description1 . '","' . $description2 . '"' . $rest;

	   echo $new_line . "<br><br><br>"; 





Open in new window

Question by:jimfrith
LVL 27

Expert Comment

ID: 22744997
>I'm using fgetcsv to import data into mysql but the csv is corrupt and I had to find a way to fix it.
What's corrupt in the line exactly?  maybe you post a sample of a non-corrupt line and a corrupt line for example.

Accepted Solution

aldanch earned 500 total points
ID: 22745032
Could you just concact the $new_line string and at the end write it out?

Once that is done you can do this:

file_put_contents("/path/to/file", $new_line);
$new_line .= $temp_string . '"' . $description1 . '","' . $description2 . '"' . $rest ."\n";

-instead of-

$new_line = $temp_string . '"' . $description1 . '","' . $description2 . '"' . $rest;

Open in new window


Author Closing Comment

ID: 31507287
Thanks that did the trick.

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now