Goth and Gore Gifts
Shop right now or else!
Viking gear
Gear Gifts and Apparel
PHP Gallery
PHP Gallery is a free php image script.
Evil gear
Christmas is almost here

Search the script archive.

Enter a search term check the script type and click submit. To search for a general category for example to find all results for scripts working with text files or file manipulation enter file or curl. Same for directory, sql etc. Otherwise use specific terms.

Here is the script you requested.

scripts/search-replace.txt: 2507 bytes
search and replace array values data base line array values

replace search array

<?php
    

$data 

file('myfile'); // reads an array of lines
function replace_a_line($data) {
   if (
stristr($data'c
ertain word'
)) {
     return 
"replacement line!\n";
   }
   return 
$data;
}

xxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

$data 
array_map('rep
lace_a_line'
,$data);
file_put_contents('myfile'implode(''$data));



xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
A quick note
PHP 5.3.0 supports lambda functions so you can
 remove the named 
function declaration and shorten the map to:

$data array_map(function($data) {

  return 
stristr($data,'certain word') ? "replacement line\n" $data;
}, 
$data);


xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You could theoretically make this a single
 
(harder to followphp statement:

file_put_contents('myfile'implode(''
  
array_map(function($d
ata
) {
    return 
stristr($data,'certain word') ? "replacement line\n" $data;
  }, 
file('myfile'))

));


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

An
other 
(less memory intensiveapproach that you should use for larger files:

$reading fopen('myfi
le'
'r');
$writing fopen('myfile.tmp''w');

$replaced false;

while (!
feof($reading)) {
  
$li
ne 
fgets($reading);
  if (
stristr($line,'certain word')) {
    
$line "replacement line!\n";
    
$replaced true;
  }
  
fputs($writing$line);
}
fclose($reading); fclose($writing);
// might as we
ll not overwrite the file if we didn't replace anything
if ($replaced) 
{
  rename('
myfile.tmp', 'my
file
');
} else {
  unlink('
myfile.tmp');
}




xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    

You c
an also use multi-line mode with regular expressions

preg_match_all('
/word}/m', $textfile, $matches
);

this is, of course, assuming it'
s a smaller document at the ready and loadedOtherwisethe oth
er answers are far more 
'real-world' of a solution.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx

$filedata 
file('filename');
$newdata = array();
$lookfor 'replaceme';
$
newtext 'withme';

foreach (
$filedata as $filerow) {
  if (
strstr($filerow$lookfor) !== false)
 
   
$filerow $newtext;
  
$newdata[] = $filerow;
}

Now $newdata contains the file contents as an ar
ray 
(use implode() if you don't want array) with the line containing "replaceme" replaced with "with
me".
?>


Post a comment

No comments yet

Search ScriptsnTips


Php JavaScripts CGI/Perl