[英]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服务器不应对该文件具有写权限
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.