【发布时间】:2011-08-25 23:42:25
【问题描述】:
我在一个目录中有大约 100 个 .php 文件,我正在寻找一个小功能,搜索这些文件的所有内容的最快方法是什么?
[编辑]
我使用的是 Windows 7 Ultimate/NuSphere PhpED。
【问题讨论】:
我在一个目录中有大约 100 个 .php 文件,我正在寻找一个小功能,搜索这些文件的所有内容的最快方法是什么?
[编辑]
我使用的是 Windows 7 Ultimate/NuSphere PhpED。
【问题讨论】:
试试这个:
<?php
function getFilesWith($folder, $searchFor, $extension = 'php') {
if($folder) {
$foundArray = array();
// GRAB ALL FILENAMES WITH SUPPLIED EXTENSION
foreach(glob($folder . sprintf("*.%s", $extension)) as $file) {
$contents = file_get_contents($file);
if(strpos($contents, $searchFor) !== false) {
$foundArray[] = $file;
}
}
if(count($foundArray)) {
return $foundArray;
} else {
return false;
}
} else {
return false;
}
}
$matched_files = getFilesWith('path/to/folder', 'Looking for this');
?>
【讨论】:
安装 cgywin - 然后你可以使用 grep!
【讨论】:
使用您的 php 编辑器“在文件中查找”功能。
无价之宝。
edit PHPNuSphere 完全支持这一点。你需要学习一些google fu哥。
如果你的编辑器没有这个,你需要尽快切换。 https://stackoverflow.com/search?q=php+editor
【讨论】:
function search_in_dir( $dir, $str )
{
$files = glob( "{$dir}/*.php" );
foreach( $files as $k => $file )
{
$source = file_get_contents( $file );
if( strpos( $source, $str ) === false )
{
unset( $files[$k] );
}
}
return array_filter( $files );
}
$files = search_in_dir( 'dir/files', 'my string' );
【讨论】:
对于 Window,我会安装 cygwin 并使用 find 或 grep,但失败了
安装总指挥官,使用 Alt + F7 递归搜索。还有一个替换多个文件选项 - http://www.ghisler.com 您会想知道在没有它的情况下您是如何导航系统的
使用Notepad++,您可以打开所有文件并进行普通文本搜索,只需选中“搜索所有打开的文件”框
【讨论】:
我使用下面的代码
<?php
$folder = 'folder';
echo '<p><b>We Find</b> in <span style="color:red">'. __DIR__ .'</span></p> ';
foreach (glob("$folder/*.php") as $filename) {
$file = file_get_contents($filename);
if( strpos( $file, 'text' ) === false ){
echo '<span style="color:red">not found: </span>';
}else{
echo '<span style="color:blue">found: </span>';
}
echo $filename.'<br>';
if (file_put_contents($filename, preg_replace("/text/", "new text", $file))) {
} else {
echo "not found :(";
}
}
在您的代码路径中创建一个文件夹
将您的文件放在该文件夹中
包含您的文本的文件以蓝色显示。然后文本将被替换
【讨论】: