find an replace multiple items

Discussion in 'PHP' started by IamNed, Oct 26, 2010.

  1. #1
    I have a long list in a txt file such as

    3434234
    34234
    eeewer
    4rw4rrr
    ytyyty
    werwe
    .
    .
    .

    and what I want to do is find and delete the lines below, for example

    34234
    eeewer
    4rw4rrr

    how is this done?
     
    IamNed, Oct 26, 2010 IP
  2. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    error_reporting(E_ALL);
    
    $file_array = file('/file.txt', FILE_USE_INCLUDE_PATH);
    
    foreach($file_array as $k => $line) {
    	
    	$line = trim($line);
    	
    	if($line == '34234' or $line == 'eeewer' /*or $line == 'xxx' .............. */) {
    		unset($file_array[$k]);
    	}
    
    }
    
    $file_array = implode("", $file_array);
    
    file_put_contents('/file.txt', $file_array, FILE_USE_INCLUDE_PATH);
    
    PHP:
     
    _:codefan:_, Oct 26, 2010 IP
  3. IamNed

    IamNed Peon

    Messages:
    2,707
    Likes Received:
    276
    Best Answers:
    0
    Trophy Points:
    0
    #3
    how about replacing 10 different lines? Would would you do? thanks
     
    IamNed, Oct 26, 2010 IP
  4. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Just put the content of these unwanted lines into the if statment
      
    [....]
    if($line == 'text1' or $line == 'text2' or $line == 'text3' or $line == 'text3' or $line == 'text4' or $line == 'text5' or $line == 'text6' or $line == 'text7' or $line == 'text8' or $line == 'text9' or $line == 'text10' or $line == 'text12' or $line == 'text13' or $line == 'text14') {
          unset($file_array[$k]);
    }
    [.....]
    
    PHP:
    or a cleaner method if you have many words

    
    error_reporting(E_ALL);
    
    $file_array = file('/file.txt', FILE_USE_INCLUDE_PATH);
    
    $unwanted = array(
    	'word',
    	'other word',
    	'xxxxxx',
    );
    
    foreach($file_array as $k => $line) {
    	
    	$line = trim($line);
    	
    	if(in_array($line, $unwanted)) {
    		unset($file_array[$k]);
    	}
    
    }
    
    $file_array = implode("", $file_array);
    
    file_put_contents('/file.txt', $file_array, FILE_USE_INCLUDE_PATH);
    
    
    PHP:
     
    Last edited: Oct 26, 2010
    _:codefan:_, Oct 26, 2010 IP
  5. RECEP

    RECEP Well-Known Member

    Messages:
    1,855
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    195
    #5
    seems clever.
    What about if we want to find and replace some css or html tags in php or html files?

    Like :
    I want to find
    and replace it with a different codes
     
    RECEP, Oct 31, 2010 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    
    
    $file = file_get_contents('file.css');
    $file = str_replace('<div class="above_body">','<div class="above_footer">',$file)
    file_put_contents('file.css',$file);
    
    PHP:
     
    ThePHPMaster, Oct 31, 2010 IP