PDA

View Full Version : PHP Remove Line Before or After String




slooksterPSV
Jan 16, 2006, 08:57 PM
Here's what I'm doing:
I've created a PHP Sidebar so that I can keep my bookmarks somewhere else besides the top. Plus it's going to serve other purposes as well, but here's my problem. I have the bookmarks storing in a bkm file (file extension I madeup) which is just a standard text file basically. Here's how it stores items:
http://www.apple.com/
Apple
http://www.google.com/
Google

Exactly like that. Now I want to remove one of my bookmarks cause I don't need it or want it anymore so let's remove Google. I want to only pass Google as the argument and have it remove the previous line from being put in the HTML file. How am I gonna do this? Here's the code I have now for what it's worth:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive">
<title>SSP</title>
<style type="text/css" media="screen"><!--
#layer1 { background-image:url('http://192.168.0.2/paper.png'); visibility: visible; position: absolute; top: 10px; left: 10px; width: 168px; height: 182px }
#layer2 { background-color:#FFFFFF; border:2px solid #000000; visibility: visible; position: absolute; top: 200px; left: 10px; width: 168px; height: 151px }
#layer3 { visibility: visible; position: absolute; top: 360px; left: 10px; width: 168px; height: 170px }
--></style>

<script language='javascript'>
function goPage(a)
{
window.open(a, '_main');
}
</script>
<?php
if(($_REQUEST['nameTB']) && ($_REQUEST['linkTB']))
{
$hdl = fopen('bookmarks.bkm', 'a');
if($hdl){
fputs($hdl, $_REQUEST['linkTB']);
fputs($hdl, "\n");
fputs($hdl, $_REQUEST['nameTB']);
fputs($hdl, "\n");
}
fclose($hdl);
}

if($_REQUEST['removeLine2'])
{
$key2 = $_REQUEST['removeLine2'];

echo $key2;

//load file into $fc array

$fc=file("bookmarks.bkm");

//open same file and use "w" to clear file

$f=fopen("bookmarks.bkm","w");

//loop through array using foreach

foreach($fc as $line)
{
if (strstr($line,$key2)) //look for $key in each line
{
echo rtrim($line);
}else{
fputs($f,$line); //place $line back in file
}
}
fclose($f);
}

?>
</head>

<body bgcolor="#9999ff" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
<div id="layer1">
<div align="center">
<font size="+5" color="#8a0124" face="Zapf Chancery, Comic Sans MS, cursive"><b><font size="5">&nbsp;Favorite &nbsp;&nbsp;Bookmarks<br>
<br>
</font></b></font>
<form>
<select name="selectName" size="1">
<option selected>Choose</option>
<?php
$hdl = fopen('bookmarks.bkm', 'r');
if($hdl){
while(!feof($hdl)){
$ln = fgets($hdl);
if($ln != ''){
echo "<option value=\"$ln\">";
}
$ln = fgets($hdl, 1024);
if($ln != ''){
echo "$ln";
echo "</option>";
}
}
}
else
{
echo "<option>CODE REVISION</option>";
}
fclose($hdl);
?>
</select><br><input type='button' value='Go!' onClick="goPage(selectName.value);"></form>
</div>
</div>
<div id="layer2">
<div align="center"><form method="post" action="sidepanel.php">
URL
<hr width="50px">
<input type="text" width="50px" name="linkTB" size="12"><br>Name
<hr width="50px">
<input type="text" width="50px" name="nameTB" size="12"><br><input type='submit' value='Add Bookmark'></form>
</div>
</div>
<div id="layer3">
<form><center>
<select name="removeLine2" size="0" >
<option selected>Choose</option>
<?php
$hdl = fopen('bookmarks.bkm', 'r');
if($hdl){
while(!feof($hdl)){
$ln1 = fgets($hdl);
if($ln1 != ''){
echo "<option value=\"$ln1\n";
}
$ln2 = fgets($hdl, 1024);
if($ln2 != ''){
echo "$ln2\">$ln2";
echo "</option>";
}
}
}
else
{
echo "<option>CODE REVISION</option>";
}
fclose($hdl);
?>
</select>
<br><input type='submit' value='REMOVE'>
</form></center>
</div>
<table width="185" border="0" cellspacing="0" cellpadding="0" cool gridx="10" gridy="10" height="768" showgridx showgridy usegridx usegridy>
<tr height="767">
<td width="184" height="767"></td>
<td width="1" height="767"><spacer type="block" width="1" height="767"></td>
</tr>
<tr height="1" cntrlrow>
<td width="184" height="1"><spacer type="block" width="184" height="1"></td>
<td width="1" height="1"></td>
</tr>
</table>
<p></p>
</body>

</html>



slooksterPSV
Jan 16, 2006, 09:06 PM
Dang I'm good, I figured out my own problem, here's what I did:
A variable was setup so each time it looped through it would increment.
When it hit the string variable it would decrement.
It would then loop throught the file, find that place and remove it like so:

if($_REQUEST['removeLine2'])
{
$key2 = $_REQUEST['removeLine2'];
echo $key2;
$fc1=file("bookmarks.bkm");
$f1=fopen("bookmarks.bkm","w");
$x = 0;
foreach($fc1 as $line1)
{
$x = x + 1;
if (strstr($line1,$key2)) //look for $key in each line
{
$x = x - 1;
}else{
fputs($f1,$line1); //place $line back in file
}
}
fclose($f1);
$num = 0;
$fc2=file("bookmarks.bkm");
$f2=fopen("bookmarks.bkm","w");
foreach($fc2 as $line2)
{
$num = $num + 1;
if ($num == $x) //look for $key in each line
{
}else{
fputs($f2,$line2); //place $line back in file
}
}
fclose($f2);
}


How smart am I? Not very if I had to ask first.


Here you guys deserve a screenshot. I like it, its easy and fun and useful as heck.

zimv20
Jan 17, 2006, 12:18 AM
so for this line:
if($_REQUEST['removeLine2'])

...what would be an example of a value for removeLine2? would it be "Google" or "http://www.google.com/"?

superbovine
Jan 17, 2006, 12:20 AM
FYI: Next use the PHP tag instead the CODE tag.

slooksterPSV
Jan 17, 2006, 02:38 AM
so for this line:
if($_REQUEST['removeLine2'])

...what would be an example of a value for removeLine2? would it be "Google" or "http://www.google.com/"?
Google

I have the value going like this:

...
echo "<option value=\"$ln2\">$ln2</option>";
...

zimv20
Jan 17, 2006, 02:55 AM
Google

so if you wanted to get rid of 'apple', but had both 'http://www.apple.com/' and 'http://www.appleinsider.com/' in your list, what would happen?

slooksterPSV
Jan 17, 2006, 03:34 AM
so if you wanted to get rid of 'apple', but had both 'http://www.apple.com/' and 'http://www.appleinsider.com/' in your list, what would happen?
If they were both named Apple then they'd both be removed. I'd put something like Apple.com or AppleInsider, be more descriptive with my name

zimv20
Jan 17, 2006, 04:06 AM
If they were both named Apple then they'd both be removed. I'd put something like Apple.com or AppleInsider, be more descriptive with my name
from the image you posted above, it looks like the delete interface is a dropdown menu. i'm assuming that means you won't be, say, typing in the name of the bookmark you want to delete, but selecting it. and to me that implies some sort of unique key as part of the datafile.

not a horribly big deal, of course, just pointing out a potential gotcha.

slooksterPSV
Jan 17, 2006, 11:28 AM
from the image you posted above, it looks like the delete interface is a dropdown menu. i'm assuming that means you won't be, say, typing in the name of the bookmark you want to delete, but selecting it. and to me that implies some sort of unique key as part of the datafile.

not a horribly big deal, of course, just pointing out a potential gotcha.
Yeah it's bad, I mean if you have two instances of one item, it removes both names, but leaves the link of the one, so you can't have two instances. This is just for my personal use. If I expand on it, I'll put it out there for everyone. I did find two errors in my code. I forgot $ on the x which made it to where it wouldn't add or subtract any #'s.

The file is Unique and if I knew how to do it better *coughs* MySQL *coughs* then I'd do it that way hehe. I'll probably move it into a MySQL database, heck I have the program on here. Files just made more sense for me.

zimv20
Jan 17, 2006, 12:50 PM
I'll probably move it into a MySQL database
i think that's a good idea. then it's easy to have a column, bookmark_id, that has unique values and the deletes will never be ambiguous.

superbovine
Jan 17, 2006, 02:11 PM
you look around sourceforge.net for 'blogroll' and 'php'. It is probably already written out there. Heck, I wrote one awhile ago. The interface for my software is a bit different, but you could produce the same results that you want.

slooksterPSV
Jan 17, 2006, 11:04 PM
you look around sourceforge.net for 'blogroll' and 'php'. It is probably already written out there. Heck, I wrote one awhile ago. The interface for my software is a bit different, but you could produce the same results that you want.
I'll take a look into that. I need to download PHPMyAdmin so that I can create tables, then google a MySQL + PHP tutorial. I've done it, but it's not sticking like C++ did.

superbovine
Jan 18, 2006, 01:04 AM
one of the best free tutorials on php + mysql, imho is at sitepoint.com. On the bottom right, there be a link to get chapters of a mysql+php book. the free chapters is enough to get you started and you can google for the rest.