简体   繁体   中英

Find and replace text in all files using PHP

Hi Everyone,

I have list of PHP files in my project.If I want to replace index.php text to home.php and save it. It will change in all my PHP file in the directory How can I code this.

I can find and replace in single file but I don't know the way to search in whole directory and replace Kindly help me to know the way. Im new to this section.Thanks in advance.

You can use glob to get all files in a directory, glob(__DIR__.'/*.php') will give you all php files in current directory. But if you need to do this in a production server only use it if you can run it as a command line function since the web server not should have write access to the files

The scandir() function might be the function you are looking for.

Returns an array of files and directories from the directory. (php.net)

If you run PHP on Linux. The quickest way to replace file content is using sed command.

find PATH -type f -exec sed -i -e 's/index.php/home.php/g' {} \\;

PHP code:

exec("find . -type f -exec sed -i -e 's/index.php/home.php/g' {} \;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM