PHP Remove Line Before or After String

Discussion in 'Web Design and Development' started by slooksterPSV, Jan 16, 2006.

  1. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #1
    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:

    Code:
    <!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"> Favorite   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>
    
     
  2. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #2
    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:
    PHP:
    if($_REQUEST['removeLine2'])
    {
    $key2 $_REQUEST['removeLine2'];
    echo 
    $key2;
    $fc1=file("bookmarks.bkm");
    $f1=fopen("bookmarks.bkm","w");
    $x 0;
    foreach(
    $fc1 as $line1)
    {
     
    $x 1;
         if (
    strstr($line1,$key2)) //look for $key in each line
         
    {
          
    $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.
     

    Attached Files:

  3. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #3
    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/"?
     
  4. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #4
    FYI: Next use the PHP tag instead the CODE tag.
     
  5. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #5
    Google

    I have the value going like this:
    PHP:
    ...
    echo 
    "<option value=\"$ln2\">$ln2</option>";
    ...
     
  6. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #6
    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?
     
  7. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #7
    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
     
  8. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #8
    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.
     
  9. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #9
    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.
     
  10. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #10
    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.
     
  11. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #11
    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.
     
  12. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #12
    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.
     
  13. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #13
    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.
     

Share This Page