hey there,

I am trying to write a simple script that lets me remove lines from a file.

I have an elementet.txt file that contains one-word lines. I thought that if I put the file lines onto an array, and then put back the array elements back on file, excluding the one I select from a drop down list, I would effectively remove a line from my file. But that is not working.

<?php
	if(isset($_POST['elem'])){//this executes if the element to be removed is selected
		$lines = file("elementet.txt");//I read the file onto an array
		$file = "elementet.txt";
		$fh = fopen($file, 'w');//open the file for writing
		
		foreach($lines as $num => $l){
		//if the line being read is not the one selected, put it back on file
			if($_POST['elem'] != $l){
				fwrite($fh, $l);
				echo $_POST['elem'];
			}
		}
		fclose($fh);
	}
?>

for some reason, my if($_POST != $l) statement is always returning true. what am I missing?

thank you!

Recommended Answers

All 7 Replies

I just modified the echo line inside my if statement, and realized that the selected element and what I thought it's corresponding element should be differ in length, the selected being one character longer. I guess I will just add an empty space in front of the character's from my file before comparison.

this is how I modified my file, and it works. (however, I believe there should be a better way and if you know of any, please let me know):

<?php
	if(isset($_POST['elem'])){
		$lines = file("elementet.txt");
		$file = "elementet.txt";
		$fh = fopen($file, 'w');
		
		foreach($lines as $num => $l){
		$l = " ".$l;//I added a leading blank character
			if(trim($_POST['elem']) != trim($l)){//I trimed both of the strings. I had to trim because putting the $l back on file without trimming messes the file up.
				fwrite($fh, trim($l)."\n");//put back newline character to make sure one line-one element file is what I get
				echo $_POST['elem'].":".$l.":".strlen($_POST['elem']).":".strlen($l)."<br />";
			}
		}
		fclose($fh);
	}
?>
Member Avatar for diafol

you could do a preg_replace();

$filename="elementet.txt";
$string = file_get_contents($filename);
$deletestring = $_POST['elem'];
$insertstring = preg_replace("/\b($deletestring)\b(\r\n)*/", "", $string);
file_put_contents($filename,$insertstring);?>

Not fully tested

thank you for your reply ardav,
however, this problem seems to come up in other places to. see this code:

first page:

<form method="post" action="form.php">
			<table>
				<?php
					$lines = file("elementet.txt");
					foreach($lines as $num => $elem){
						echo "<tr><td>".$elem.":"."</td>";
						echo "<td><input type='text' name='{$elem}' /></td></tr>";//I GENERATE HERE NAMES FOR INPUT BOXES BASED ON MY elementet.txt file
						echo $elem;
					}
				?>
			</table>
			<input type="submit" value="Shto anetarin" />
		</form>

form.php is the following:

$lines = file("elementet.txt");
	$length = sizeof($lines);
	echo $length;
	$file = "family.xml";
	$fh = fopen($file, 'a');
	fwrite($fh, "\n<anetari>\n");
	$emri = 'emri';
	foreach($lines as $num => $l){
		fwrite($fh, $_POST[$l]);//I GENERATE HERE INDEXES BASED ON MY ELEMENTET.TXT FILE, BUT I GET THE COMPLAINT THAT THE INDEX IS UNDEFINED
	}
	
	fwrite($fh, "</anetari>");
	fclose($fh);

any idea what's going on?

Member Avatar for diafol

or for a single line:

file_put_contents('text.txt',preg_replace("/\b({$_POST['elem']})\b(\r\n)*/", "", file_get_contents('text.txt')));

:) - seriously, I wouldn't normally code like that!

//EDIT

Ok saw your last post, will have a think.

Your static data (form fields) is in the txt file and you want to append the chosen data from the form into the xml file. Is that right?

Do a

print_r($_POST);

to see which fields are being passed.

BTW:

$emri = 'emri';

doesn't seem to be doing anything.

ok, yes. there are around three to five lines that do nothing.

this is what I am trying to do. I am trying to build a small app for genealogy. Initially, I identified info I needed: name, last name (for wives/mothers; patriarchal based), date of birth, place of birth, a node id, a parent id (lest two individuals have the same name, based on a parent's node id, I'd identify who is whose father), and such. however, I thought of making the app expandable by increasing the number of elements that represent information on an individual. so, i though I should just save the elements I want to be available on a separate text file, and then, when I build the form for taking in information, I would build an input box for each line/element on the elementet.txt file. AND, I thought I could use the same list of elements from that file to build indices for $_POST[]. however, seems like info extracted from the file and saved on to the array with file($file) is different from what I assumed it would be. or am I wrong on this too?

Member Avatar for diafol

Using arrays may be more useful than text files. A DB should be used for saving info of this type. If you need an extra field, you simply add one to the DB and a filed to your array. XML (IMO) is an intermediate source/storage used for passing between systems/sites.

thank you ardav for trying to help. I know that xml is not the most suitable solution for this kind of a problem, but...

I found a solution for my problem. I needed to trim $elem on line 7 of my first file. as well as $l variable in my second file, and it all works fine.

thank you again.

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.