Text replacement, regexp

Discussion in 'PHP' started by Airwave, Mar 8, 2009.

  1. #1
    Hi, i need some help.
    I have list like this:

    
     22. darkcenter.net <http://darkcenter.net/>
     23. daviefla.biz <http://daviefla.biz/>
     24. de.world-searchengines.org <http://de.world-searchengines.org/>
    
    Code (markup):
    and so on, about 8000 lines. What i need is replace everything except link between < > and write that link into file, all 8000 lines, with php. I forgot regexp syntax, tried to remember but failed :D so please write code for this sh*t :rolleyes:
     
    Airwave, Mar 8, 2009 IP
  2. nosf009

    nosf009 Well-Known Member

    Messages:
    146
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    118
    #2
    
    <?
    
    $file_to_read = "lines.txt";
    $file_to_write = "output.txt";
    
    
    $fh = fopen("$file_to_read", "r");
    $i = 1;
    while ($line = fgets($fh)) {
    $row[$i] = $line;
    $i++;
    }
    fclose($fh);
    
    
    
    $fh = fopen($file_to_write, 'w') or die("can't open file");
    		
    	$a = 1;
    	$max = count($row);
    	
    	while ($a <= $max) {
    	
    		$var[$a] = explode(" ",$row[$a]);
    	
    		$data[$a] = "<a href='http://".$var[$a][1]."'>".$var[$a][1]."</a>\n";
    		
    		echo $data[$a];
    		
    		/*
    		fwrite($fh, $data[$a]);
    		*/
    		
    		
    		
    		$a++;
    		}
    
    #print_r($row);
    ?>
    
    PHP:
    We'll, I'm not much good with regex's but, this above should do the trick.
    Let me know if I need to modify anything. Oh, yeah, remember to uncomment part where it writes to file. What is pasted here could be used for browser output, for test.
    Also, if you don't need standard link syntax for html, you can modify line where $data variable is being constructed to suit you.
     
    nosf009, Mar 8, 2009 IP
  3. Airwave

    Airwave Guest

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks dude, modified little bit but still works like i need ;)
     
    Airwave, Mar 8, 2009 IP