delete data from file
Moderator: General Moderators
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
delete data from file
Hey guys, I've been looking for a function to delete data from a file but having trouble. Can someone link me to a function to set me on the right track?
My file is a .htpasswd file so the contents look like this:
joesmith01:rlY/Eb2xa0uYs
johnny92:rlO0/FP9K1Dqc
example3:rlbVJNbKNDKc2
I want to be able to delete users (the user:pass line) via my script. Would I pull the data as an array and use array_splice() or what would you guys think?
Thanks!
My file is a .htpasswd file so the contents look like this:
joesmith01:rlY/Eb2xa0uYs
johnny92:rlO0/FP9K1Dqc
example3:rlbVJNbKNDKc2
I want to be able to delete users (the user:pass line) via my script. Would I pull the data as an array and use array_splice() or what would you guys think?
Thanks!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
Ok, I kept getting problems so I narrowed it down to the unset() function. I'm using the example on php.net and have no idea why its not working. I need to be able to delete single elements of an array, while leaving the array intact.
Returns:
The example given at php.net is this:
Code: Select all
<?php
$array = array("red", "green", "blue", "yellow", "black");
echo "Before: " . $array[0] . ", " . $array[1] . ", " . $array[2] . ", " . $array[3] . ", " . $array[4];
unset($array['green']);
echo "<br>After: " . $array[0] . ", " . $array[1] . ", " . $array[2] . ", " . $array[3] . ", " . $array[4];
?>Returns:
Before: red, green, blue, yellow, black
After: red, green, blue, yellow, black
The example given at php.net is this:
Code: Select all
// destroy a single element of an array
unset($bar['quux']);-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
I hate to keep posting about this but I think I've been looking at this code so long I can't see whatever is wrong.
I can't get the stristr() function to find my results from the values of my array. Here is my code:
My test username is johnny92. Thats what $f3[1] shows before the stristr function, then it shows nothing. Basically I need to get the stristr() to give me the full array value for johnny92 (which would be johnny92:rlO0/FP9K1Dqc, so I can remove it from the array.
I can't get the stristr() function to find my results from the values of my array. Here is my code:
Code: Select all
<?php
$date = date("Ymd");
$db = 'XXXXXX'; // mySQL database name
$duser = 'XXXXXX'; // mySQL username
$dpw = 'XXXXX'; // mySQL password
$mysql_access = mysql_connect("localhost", $duser, $dpw);
mysql_select_db($db, $mysql_access);
$queryz2 = "SELECT * FROM promos WHERE expiration > $date ";
$resultz2 = mysql_query($queryz2, $mysql_access);
$lines = file('.htpasswd');
echo "Array Before:<br>" . $lines[0] . "<br>" . $lines[1] . "<br>" . $lines[2] . "<br>" . $lines[3] . "<br>" . $lines[4] . "<p>";
/* returns:
john9382x35:rlY/Eb2xa0uYs
johnny92:rlO0/FP9K1Dqc
exp333:rlbVJNbKNDKc2
user911:rlSRB4ZmA.W1U
*/
$i = 1;
while($row = mysql_fetch_row($resultz2))
{
$f1[$i] = $row[0];
$f2[$i] = $row[1];
$f3[$i] = $row[2];
$f4[$i] = $row[3];
}
echo "test user: " . $f3[1]; // returns "johnny92"
$i = 0;
$z = 1;
foreach ($lines as $line_num => $line) {
$fd1 = stristr($lines[$i], $f3[0]);
$fd2 = stristr($lines[$i], $f3[1]);
$fd3 = stristr($lines[$i], $f3[2]);
$fd4 = stristr($lines[$i], $f3[3]);
echo "<p>matched results:<br>" . $fd1 . "<br>" . $fd2 . "<br>" . $fd3 . "<br>" . $fd4 . "<p>"; // returns nothing
if ($lines[$i] == $fd1 or $lines[$i] == $fd2 or $lines[$i] == $fd3 or $lines[$i] == $fd4) {
unset($lines[$i]);
$lines = array_values($lines);
$z++;
$i++;
}
}
echo "Array After:<br>" . $lines[0] . "<br>" . $lines[1] . "<br>" . $lines[2] . "<br>" . $lines[3] . "<br>" . $lines[4] . "<p>";
/* returns:
john9382x35:rlY/Eb2xa0uYs
johnny92:rlO0/FP9K1Dqc
exp333:rlbVJNbKNDKc2
user911:rlSRB4ZmA.W1U
*/
?>-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
This is what is not working:
Is this because $lines[$i] is a value of an array and not a string? Or isn't it a string in this case? 
Code: Select all
$fd1 = stristr($lines[$i], $f3[0]);