I'm rather new to PHP, and was trying to remove all lines where any instance of the string variable 'user' appears. My current code
if($action == "removeUser")
{
foreach(file('users.txt') as $line)
{
if (strpos($line, $parameters) !== false)
{
$line = "";
}
}
}
For some reason this doesn't seem to have any effect at all. What am I doing wrong?
You need to open the file and read the lines.
<?php
if($action == "removeUser")
{
$filename = "users.txt";
// Open your file
$handle = fopen($filename, "r");
$new_content='';
echo "Valid input: <br><br>";
// Loop through all the lines
while( $line = fgets($handle) )
{
//try to find the string 'user' - Case-insensitive
if(stristr($line,"user")===FALSE)
{
// To remove white spaces
$line=trim($line);
if($line!='') echo $line."<br>";
//if doesn't contain the string "user",
// add it to new input
$new_content.=$line."
";
}
}
// closes the file
fclose($handle);
$new_content=trim($new_content); // Remove the
from the last line
echo "<br>Updating file with new content...";
file_put_contents($filename,$new_content);
echo "Ok";
}
?>