Page 1 of 1

Deleting Specified Text

Posted: Sun Apr 16, 2006 10:33 am
by GRPsuper
lets say for example, in a php file i had this written in it:
==Start Example==

1
2
3
4
5
6
7
8
9
10

==End Example==

what i want to do, is i only want to remove ONE number that i select, so when i type http://www.site.com/numbers?delete=8, it would delete the number 8. how would i do this?

ONLY SPECIFIED TEXT IS WHAT I WANT TO DELETE, NOT THAT WHOLE THING!

ight, thanks if u help out ;)

Posted: Sun Apr 16, 2006 10:39 am
by feyd
What's supposed to happen if delete equals 1?

Posted: Sun Apr 16, 2006 11:10 am
by GRPsuper
then it deletes the number 1

Posted: Sun Apr 16, 2006 11:21 am
by feyd
It wouldn't affect 10 as well?

What if you have two or more entries that match delete?

Posted: Sun Apr 16, 2006 11:22 am
by GRPsuper
then it would remove the repeated numbers as well

Posted: Sun Apr 16, 2006 11:42 am
by Benjamin
1. Load the file into a variable
2. Load the number into another variable
3.

Code: Select all

// if it has a line feed after it you may want to uncomment this line
//$NumberToDelete = $NumberToDelete . "\n";
$NewFile = str_replace($NumberToDelete, null, $OldFile);
4. Save the file

Posted: Sun Apr 16, 2006 11:45 am
by GRPsuper
thanks, that is what i want, but how would i make it delete what i made "delete="? or is the code already in that?

Posted: Sun Apr 16, 2006 12:02 pm
by Benjamin

Code: Select all

$NumberToDelete = $_GET['Delete'];

Posted: Sun Apr 16, 2006 12:06 pm
by Oren
But what if other places in the file contain '1' for example?

Posted: Sun Apr 16, 2006 12:08 pm
by Benjamin
Good call,

That will delete all the 1's in the file for example. You may want to ensure that all the data in the file is prepended and appended by a space or some other character.

Code: Select all

$NumberToDelete = ' ' . $NumberToDelete . "\n ";
Look into explode();

Just download a copy of the PHP manual and you'll be good to go.

Posted: Sun Apr 16, 2006 12:20 pm
by GRPsuper
thanks, where can i get the php manual? on this site? btw, this is out of topic, but are u the creator of this forum?

EDIT: nvm, found it on ur signature lol.

Posted: Sun Apr 16, 2006 12:56 pm
by GRPsuper
wait, i put the thing in there, but im kinda confused cuz i dont think its in the right order. and i also dont know what parts of the code i should take out. can anyone help? this code is for trying to take out the IPs. cuz i have 2 files, ban.php and deleteip.php. ban.php is where all the ips are stored and deleteip.php is where i will be able to delete them.

here is an example of what ban.php looks like:

127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
68.0.5.6

and i want deleteip.php?delete=127.0.0.3 to delete the ip "127.0.0.3" maybe this will help you guys alittle more on what i need?

Posted: Sun Apr 16, 2006 1:10 pm
by feyd
Why not use a database for this?

This is entirely untested.

Code: Select all

if (isset($_GET['delete']) and preg_match('#^(?:\d{1,3}\.){3}\d{1,3}$#s', $_GET['delete']))
{
  $delete = $_GET['delete'];
}
else
{
  die('invalid input');
}

$file = file('banip.php');
$file = array_map('trim', $file);
$file = array_filter($file, create_function('$a', 'return $a != ' . var_export($delete,true)));
$file = array_filter($file, 'strlen');

$fp = fopen('banip.php', 'wt');
fwrite($fp, implode("\n", $file));
fclose($fp);

Posted: Sun Apr 16, 2006 7:48 pm
by timvw
I recommend that you use [url]http://www.php.net/ip2long[url] and then use the integers to do the comparison...

Posted: Sun Apr 16, 2006 8:47 pm
by Benjamin
Looks like feyd spent some time to write some good code for you. You might want to spend some time looking up all the functions in the manual to understand it better.