Detecting URL in a text file

Discussion in 'PHP' started by MrLeN, Aug 6, 2008.

  1. #1
    
      $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/categories/'.$_GET['category'].'/'.$_GET['subcategory'].'/data.txt', 'w');
    
      if ($fp === false) die("Couldn't open data.txt for writing!");
    
      foreach ($domains as $dom => $count) {
    	
    	  [color=red]if(eregi($dom,$_SERVER['HTTP_HOST'])) {
           //I WANT TO REMOVE THE LINE
    		}
        else {[/color]
        fwrite($fp, "$dom $count\n");
    		[color=red]}[/color]
    
      }
    
      fclose($fp);
    
    Code (markup):
    In the above code, I want to remove a line (within data.txt) if it contains the current domain name. I have added the parts in red. What am I doing wrong?
     
    MrLeN, Aug 6, 2008 IP
  2. sandeepb

    sandeepb Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well you can do something to remove the file with unlink() and just recreate the text file.
     
    sandeepb, Aug 6, 2008 IP
  3. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #3
    I already have functions to delete and recreate the file. However, when the file is being created, I want to detect if certain lines contain HTTP_HOST and remove those lines.
     
    MrLeN, Aug 6, 2008 IP
  4. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not sure I completely understand what you're trying to do, it sounds like what you want can be accomplished by using file_get_contents to read the file into a string, using eregi_replace, and then writing the string back into the file.
     
    Shoro, Aug 6, 2008 IP
  5. CarcaBot

    CarcaBot Active Member

    Messages:
    389
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    try something like this
    
    preg_match("/http(s)?:\/\/(*.?)\s/", $stringUrl, $matches)
    
    PHP:
    /CarcaBot
     
    CarcaBot, Aug 6, 2008 IP
  6. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #6
    never mind I had someone help me..

    I was on the right track.

    To do what I wanted the code is:

    
    if(eregi($_SERVER['HTTP_HOST'], $dom)) {
           echo "";
    		}
        else {
        fwrite($fp, "$dom $count\n");
    		}
    
    Code (markup):
     
    MrLeN, Aug 6, 2008 IP
  7. Jackel.ca

    Jackel.ca Well-Known Member

    Messages:
    108
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #7
    
    if (!eregi($_SERVER['HTTP_HOST'], $dom))
    	fwrite($fp, "$dom $count\n");
    
    PHP:
    Should accomplish the same, without the unecessary echo.
     
    Jackel.ca, Aug 6, 2008 IP
  8. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Do not use regular expressions for these 'small' tasks. Use str_replace() instead. Regex are more resource consuming and powerful, both of which are not needed for your case.

    Also, if you insist on regex, then use preg_* functions instead of ereg() functions. Ereg functions have some bugs/flaws.
     
    rohan_shenoy, Aug 6, 2008 IP