Solved

replace line in a text file php

Posted on 2008-10-17
3
877 Views
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);
				$needle=",";
				$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.
				$temp_string=substr($haystack,0,$position+1);
				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>"; 
    }
    fclose($handle);
}
?>

Open in new window

0
Comment
Question by:jimfrith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Expert Comment

by:ddrudik
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.
0
 
LVL 8

Accepted Solution

by:
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

0
 

Author Closing Comment

by:jimfrith
ID: 31507287
Thanks that did the trick.
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

635 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