Jump to content

edit text file in php


boneXXX

Recommended Posts

I am trying to find a specific line, which starts with a unique word, then delete that line in the .txt file then rewrite that line. Could you help me on this please ?

 

 

.txt file

John|Soccer player
Smith|journalist

 

<?php

$username_tmp= "Smith" ;

$listf = fopen ("./users.txt", "r+") or die("error openning file");

list($username,$profile) = fgetcsv($listf, 4096, "|");	

while(!feof($listf))
{
if ($username_tmp == "Smith" )
{
              ....................  
      	}
         list($username,$profile) = fgetcsv($listf2, 4096, "|");		
}
fclose($listf);
?>

 

I can not go further than this I need help, thanks in advance.

Link to comment
Share on other sites

The only way to look up records or contents in a file is by using fseek(); . You would have to know where the information you are looking for is and then do the rewrite.

 

Might I suggest converting the contents of the file into a string then do your manipulations and afterwords rewite it back to the file.

Link to comment
Share on other sites

Unfortunately I can't use "sed".

 

I tried to put new info in a line then separate them in to arrays. Then find which line I want to update then write it.

What function do you recommend for writing? just fwrite() or something else. There is no specific error just it doesn't work.

 

//open the files
$listf1 = fopen ("../datafile/member.TXT", "r+") or die("error opnenning file");
//read the files
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
        
       //find the username to update info
while(!feof($listf1))
{
	if ($username_tmp == $username )
	{

                         // assign new info from the forum
                        $new_password = strip_tags(trim($_POST['PassWD']));
		$new_first_name = strip_tags(trim($_POST['first_name']));
		$new_last_name = strip_tags(trim($_POST['last_name']));
		$new_email = strip_tags(trim($_POST['email']));
		$new_organization =  strip_tags(trim($_POST['organization']));
		$new_profile = $_POST['profile'];
		$user_level = 2;

		$line_new = "$username|$new_password|$new_last_name|$new_first_name|$new_email|$new_organization|$user_level";		
	}
	list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");		
}	
//close the files
fclose($listf1);

        //separate them and put them into an array		
$split_it = strtok($line_new, "|");
while ($split_it != false)
{
	$n = 0;
	$get_in[$n] = "$split_it";
	$n = $n++;	
}	

$listf1 = fopen ("../datafile/member.TXT", "w+") or die("error opnenning file");
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
while(!feof($listf1))
{
	if ($username_tmp == $username )
	{	
		for ($n=0; $n < 6; $n++)
		{
			fputs($line_new,$get_in[$n]);//replace the info with new info
		}
	}	
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
}
fclose($listf1);

        //refresh the page again with the new profile info 
        header("location: ./view_profile.php");

Link to comment
Share on other sites

I have just dealt with the same problem but I am using a sql db. The problem was the same, delete only certain text from a text source then rewriting it.

 

I do believe that it could be of use to you since the text is stored as a string then exploded into an array then deleted and then rewritten.

 

Here is a link to that post, I hope this could help you, if you would like help adapting to your scenerio, pm and I will work it out for you.

 

http://www.phpfreaks.com/forums/index.php/topic,137307.0.html

 

Richard

Link to comment
Share on other sites

I am getting close to the end I think. The problem is know, it doesn't rewrite but it wipes everything in the .txt file.

 

<?php


  	//open the files
$listf1 = fopen ("../datafile/member.TXT", "r+") or die("error opnenning file");
//read the files
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
        
    //find the username to update info
while(!feof($listf1))
{
	if ($username_tmp == $username )
	{
            // assign new info from the forum
            $new_password = strip_tags(trim($_POST['PassWD']));
		$new_first_name = strip_tags(trim($_POST['first_name']));
		$new_last_name = strip_tags(trim($_POST['last_name']));
		$new_email = strip_tags(trim($_POST['email']));
		$new_organization =  strip_tags(trim($_POST['organization']));
		$new_profile = $_POST['profile'];
		$user_level = 2;

		$line_new = "$username|$new_password|$new_last_name|$new_first_name|$new_email|$new_organization|$user_level";		
	}

	//separate them and put them into an array		
	$split_it = strtok($line_new, "|");
	while ($split_it != false)
	{
		$n = 0;
		$get_in[$n] = "$split_it";
		$n = $n++;	
	}	
	list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");		
}	
//close the files
fclose($listf1);

$listf1 = fopen ("../datafile/member.TXT", "w+") or die("error opnenning file");
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
while(!feof($listf1))
{
	if ($username_tmp == $username )
	{	
		for ($n=0; $n < 6; $n++)
		{
			fputs($line_new,$get_in[$n]);//replace the info with new info
		}
	}	
list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");
}
fclose($listf1);

    //refresh the page again with the new profile info 
    echo "<p><center>You have successfuly add the member to the database.<br><a href='./view_profile.php'>Click to Continue</a><p></center>";

?>

Link to comment
Share on other sites

<?php

 

// assign new info from the forum

    $username_tmp=$_SESSION['valid_user'];

$new_password = strip_tags(trim($_POST['password']));

$new_first_name = strip_tags(trim($_POST['first_name']));

$new_last_name = strip_tags(trim($_POST['last_name']));

$new_email = strip_tags(trim($_POST['email']));

$new_organization =  strip_tags(trim($_POST['organization']));

$new_profile = $_POST['profile'];

$user_level = 2;

 

  //open the files

$listf1 = fopen ("../datafile/member.TXT", "r+") or die("error opnenning file");

//read the files

list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|"); 

 

    //find the username to update info

while(!feof($listf1))

{

if ($username_tmp == $username )

{

$line_new = "$username|$new_password|$new_last_name|$new_first_name|$new_email|$new_organization|$user_level";

echo "$line_new<br>";

 

$profile_inf = explode("|", $line_new);

 

for($x = 0; $x < count($profile_inf); $x++)

{

echo "Piece $x = $profile_inf[$x] <br />";

}

}

list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");

}

//close the files

fclose($listf1);

 

//open the file to write

$listf1 = fopen ("../datafile/member.TXT", "w+") or die("error opnenning file");

list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");

while(!feof($listf1))

{

if ($username_tmp == $username )

{

for ($x=0; $x < count($profile_inf); $x++)

{

fputs($listf,$profile_inf[$x]);//replace the info with new info

}

}

list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");

}

fclose($listf1);

 

//$listf1 = fopen ("../datafile/member.TXT", "w+") or die("error opnenning file");

//list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");

//while(!feof($listf1))

//{

//if ($username_tmp == $username )

//{

//for ($n=0; $n < 6; $n++)

//{

//fputs($line_new,$get_in[$n]);//replace the info with new info

//}

//}

//list($username,$password,$last_name,$first_name,$email,$organization,$user_level) = fgetcsv($listf1, 4096, "|");

//}

//fclose($listf1);

 

    //refresh the page again with the new profile info

    //echo "<p><center>You have successfuly add the member to the database.<br><a href='./view_profile.php'>Click to Continue</a><p></center>";

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.