Advertisement

12.11.2007 at 09:41AM PST, ID: 23016160
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Using php delete line from txt file.

Asked by justmelat in PHP Scripting Language

Tags: , , , ,

I am doing some forward thinking on a project i'm working on.

With the help of some gurus here I got the attached code working perfectly.  I am anticipating that my boss will want to be able to delete a word from this text file at some point.

I came across this set of instructions while trying to figure out if it is possible.
Use filegetcontents() to read the file into a string.

Use explode() to get the contents of the file/string into an array (since they're just single items, right?).

Loop through the array to remove what you wanted to remove.

Output the array (using implode()) back to the file.

My questions:

1) is it possible to delete a line from a text file using php?
2) are the instructions above doable?
3) if they are doable how would i code it and incorporate it into the code attached?

I am thinking that I will have button that takes the user to another page where a dropdown box containing the results (contents of
txt file, which is just one column of names) is located, then they just select the words they want removed from the text file.

I know u are saying, it should be in a db, but for some unknown reason, they want this file to remain a txt file.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
<?php
 
session_start();
$phpself = $_SERVER['PHP_SELF'];
 
if ($_POST['btnAddWord'] == 'Add' )
	{
		$newWord = trim($txt_newWord);
		//echo "Word to be added: ".$newWord."<br><br>";
 
		$myFile = 'filterlist.txt';
		//echo $myFile."<br><br>";
		
		file_put_contents($myFile, $newWord . PHP_EOL, FILE_APPEND) or die("can't open file");
		
		/*$fh = fopen($myFile, 'a') or die("can't open file");
		
		$stringData = $newWord;
		fwrite($fh, $stringData);*/
		
		//fclose($fh);
		echo "<b><font size=4 color=#000080>New Word Successfully added to Bad Word Filter List.</b></font>";
		echo "<script>setTimeout(\"document.location.href = 'badWordFilter.php'\",2000)</script>";
 
	}
	else 
{
		
	echo "<b><font size=5 color=#000080>Bad Word Filter</font></b><br>";
 
	echo "<form method=POST action=$phpself name=frmWordFilter>";
	
	
	echo "<br><b>Add New Word to Filtered List:</b><br><input type=text name=txt_newWord size=35>";
	echo "<p><input type=submit value=Add name=btnAddWord></p>";
	echo "</form>";
echo "<hr color=#000080 width=85% size=3>";
$myFile = "filterlist.txt";
$fh = fopen($myFile, 'r');
$theData = implode(', ', file($myFile));
 
 
$lines = file('filterlist.txt');
echo "<br><b>Current List:  </b><br><br>";
// Loop through our array, show HTML source as HTML source; and line numbers too.
 
foreach ($lines as $line_num => $line) 
{
	echo "Word #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
 
// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('filterlist.txt'));
 
//$theData = implode(",", fread($fh, filesize($myFile)));
fclose($fh);
 
echo "<br><b>Current List:  </b>".$theData;		
}
 
?>
 
Loading Advertisement...
 
[+][-]12.11.2007 at 11:37AM PST, ID: 20451954

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Tags: php, line, delete, file, from
Sign Up Now!
Solution Provided By: Michael701
Participating Experts: 4
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628