To find old text in files and replace it with new text using the shell_exec
function in PHP, you can use the sed
command in Linux/Unix systems. Here is an example code snippet:
$old_text = "old text";
$new_text = "new text";
$path_to_files = "/path/to/files/";
// Use sed to find and replace text in all files in the specified path
$output = shell_exec("sed -i 's/$old_text/$new_text/g' $path_to_files*");
// Display the output of the sed command
echo $output;
This code will search for the specified old_text
in all files in the specified path_to_files
, and replace it with the new_text
. The -i
option in the sed
command specifies that the search and replace should be done in-place in the original files, and the g
flag specifies that all occurrences of the old text should be replaced (not just the first occurrence).
Note that using shell_exec
function can be potentially dangerous if you pass untrusted data as arguments to the command. It is important to properly sanitize and validate user input before using it in this way to avoid shell injection vulnerabilities.