Hi,
i have query, i want to replace a string in text file. it should write correctly in specified line in the text file . whether it is possible.

my doubt is if i delete the line in the text file. it remains blank. it should not occupy the other rows. in that case how i should replace a string in specified location in the line.

help me.

thanks for your reply.

Try something like this:

<?php
$line=82; //includes zero as 1.
$newdata='this is a test 123';
$data=file('path/to/file.txt');
$data[$line]=$newdata."\r\n";
$data=implode($data);
file_put_contents('path/to/output.txt',$data);

i'm new to this field,
so i don't have that much stuff.
i tell my status.

this is text my file.
example.(file.txt)
line1:119,mohan,male,2-01-1000,1-1-2001,5000,kovalam,chennai
line2:118,murugan,male,2-01-1000,1-1-2001,5000,madurei
line3:116,folo,male,2-01-1000,1-1-2001,1000232.
line4:117,rani,female,2-01-1000,1-1-2001,17000,

i want to change the line 3: to some new data.

$file ='\filetxt';
if(file_exists($file))
			  {
				  //Then you attempt to open the file, in this case for reading.
				  try{
					    $lines = count(file($file));  
						// $lines=$lines-1;
						// echo "There are $lines lines files<br/>";
						 
						if($openfile= fopen($file,"r+"))
							{
								while(!feof($openfile)) 
								{
									
									//for($i=0;$i<$lines;$i++)
									//{									
											$read= fgets($openfile);
											$row = explode(",", $read);
										 	//if($row[0]==$ap[0])
											$vv=$row[0];
											if($vv=='118')
										    {
												echo "the line$i" . $row[0];
						
												// for($j=0;$j<count($row);$j++)
												// {  $row[$j]='';
												 //}exit;
												  for($j=0;$j<count($row);$j++)
												 { 
													$dp[$j]=$ap[$j];
													unset($row[$j]);
												 }
												
												  $writestr = $dp[0] . "," . $dp[1] . ",". $dp[2] ."," . $dp[3] . "," . $dp[4] ."," . $dp[5] . "," . $dp[6] . "," . $dp[7] . "," . $dp[8] . "," . $dp[9]. "," . $dp[10] . ",". $dp[11] .".";
												  fwrite ($openfile, $writestr);
												  echo " successfully written your file";	 
												  exit;
								        	}
												 
									//}
											//echo"not compare";
										          
								}

							}
							else
							{
								//if it fail then throw exception
								throw new exception ("sorry , file could not be opened");
							}
				        
				     
					  fclose($openfile);   
					 }
					 catch(exception $e)
					 {
						 echo $e->getmessage();
						 
					 }
			  }
			  else
			  {
				  echo "File doesn't exists";
			  }

when i run the code.
output shown like this:
119,mohan,male,2-01-1000,1-1-2001,5000,kovalam,chennai,malasiya,singapore,9883763772,+91444423232.
118,murugan,male,2-01-1000,1-1-2001,5000,madurei,chennai,japan,singapore,9883763772,+91444423232.
118,folo,male,2-01-1000,1-1-2001,1000,,,,,,.oney,chennai,australia,singapore,9883763772,+91444423232.
117,rani,female,2-01-1000,1-1-2001,17000,cuddalore,chennai,us,europe,9883763772,+91444423232.32.

what's wrong in this code. i will do change in my code

help me................

thanks for reply.

Could you tell me how you want to display your output? and what is $ap array in your script.

1.
      <?php
   2.
      $line=82; //includes zero as 1.
   3.
      $newdata='this is a test 123';
   4.
      $data=file('path/to/file.txt');
   5.
      $data[$line]=$newdata."\r\n";
   6.
      $data=implode($data);
   7.
      file_put_contents('path/to/output.txt',$data);

i got the output >>

i 'didn't use file_put_contents it's work fine.

Could you tell me how you want to display your output? and what is $ap array in your script.

$ap is replacing string i get in the array
there is nothing else.

Could you tell me how you want to display your output? and what is $ap array in your script.

could you help me to delete the line in the text file using php
just give guide lines

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.