Deleting Specified Text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Deleting Specified Text

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's supposed to happen if delete equals 1?
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Post by GRPsuper »

then it deletes the number 1
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It wouldn't affect 10 as well?

What if you have two or more entries that match delete?
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Post by GRPsuper »

then it would remove the repeated numbers as well
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$NumberToDelete = $_GET['Delete'];
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

But what if other places in the file contain '1' for example?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Post 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.
GRPsuper
Forum Newbie
Posts: 18
Joined: Fri Apr 14, 2006 9:47 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I recommend that you use [url]http://www.php.net/ip2long[url] and then use the integers to do the comparison...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Post Reply