: This article describes how to search and replace php in full text. For more information about PHP tutorials, see. Php full-text search and replacement
Exec ("/bin/grep-R' $ oldword' $ rootpath", $ results, $ errorCode );
If ($ errorCode ){
If ($ errorCode = 1 ){
Echo "Possibly no files were found with $ oldword in them
\ N ";
}
Echo "OS Error: $ errorCode
\ N ";
Echo "Check 'Man errno' and count down
\ N ";
Echo "Usually paths/permissions
\ N ";
}
While (list (, $ path) = each ($ results )){
$ Parts = explode (':', $ path );
$ Path = $ parts [0];
$ Fp = fopen ($ path, 'r') or PRint ("Cannot read $ path
\ N ");
If ($ fp ){
$ Data = fread ($ fp, filesize ($ path ));
Fclose ($ fp );
$ Newdata = str_replace ($ oldword, $ newword, $ data );
$ Fp = fopen ($ path, 'w') or print ("Cannot write $ path
\ N ");
If ($ fp ){
Fwrite ($ fp, $ newdata );
Fclose ($ fp );
Echo $ path ,"
\ N ";
}
}
}
?>
Example
Http://yourserver.com/globalreplace.php? Oldword = test & newword = text & rootpath =/path/to/dir
The above describes how to perform full-text search and replacement in php, including the full-text search and replacement content, and hopes to help those who are interested in PHP tutorials.