简体   繁体   English

使用PHP查找并替换所有文件中的文本

[英]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. 我的项目中有PHP文件列表,如果我想将index.php文本替换为home.php并保存它。 It will change in all my PHP file in the directory How can I code this. 我的所有PHP文件都将在目录中更改。如何编码。

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. 您可以使用glob获取目录中的所有文件, glob(__DIR__.'/*.php')将为您提供当前目录中的所有php文件。 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 但是,如果您需要在生产服务器中执行此操作,则仅当您可以将其作为命令行功能运行时才使用它,因为Web服务器不应对该文件具有写权限

The scandir() function might be the function you are looking for. scandir()函数可能是您要寻找的函数。

Returns an array of files and directories from the directory. 从目录返回文件和目录的数组。 (php.net) (php.net)

If you run PHP on Linux. 如果您在Linux上运行PHP。 The quickest way to replace file content is using sed command. 替换文件内容的最快方法是使用sed命令。

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

PHP code: PHP代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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