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?
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:
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:
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
$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: