【问题标题】:Use PHP to search directorys for files recursively and change values使用 PHP 递归搜索目录中的文件并更改值
【发布时间】:2017-02-25 16:34:37
【问题描述】:

我有一些 php 替换目录中 css 文件中的颜色值。它工作正常,只是它只在主目录中搜索 css 文件。我一直在寻找,但似乎无法找到一种简单的方法来进行递归。有什么想法吗?

我目前正在使用 $arr=glob("themes/Evolution/*.css");

具有 css 文件的示例文件夹结构是。

Main Dir > Style.css
Main Dir > Folder 1 > mycss.css
Main Dir > Folder 2 > mycss.css
Main Dir > Folder 3 > mycss.css

//Read default color from INI
$lines_array = file("modules/evolution/evolution.ini");
$search_string = "currentcolor";

foreach($lines_array as $line) {
    if(strpos($line, $search_string) !== false) {
       list(, $new_str) = explode("=", $line);
          $new_str = trim($new_str); //This line removes the spaces before and after.
    }
}

$inicurrentcolor = $new_str;

//code to change css color
if(isset($_REQUEST['resetcolor'])){
	$arr=glob("themes/Evolution/*.css"); //your css file's path
		$textboxdefaultcolorcode=$_POST['defaultcolor']; 
		
foreach($arr as $key=>$val){
	$str=file_get_contents($val);
			$str=str_replace($inicurrentcolor, $textboxdefaultcolorcode, $str); 
		file_put_contents($val, $str);
	}
}

<div class="colorbox">
<br>
<label id="steps">Reset Evolution theme color back to the default color.</label>
<br>
<br>
<Form name="default1" method="POST" action="home.php?m=evolution">
<label for="color1">Default Theme Color: </label><input style="background-color:#379BB9; color:#ffffff" type="text" id="defaultcolor" name="defaultcolor" value="#379BB9" readonly>
<br>
<br>
<input type="submit" name="resetcolor" value="Reset Theme Color">
</form>
<br>
</div>

【问题讨论】:

    标签: php html css var


    【解决方案1】:

    我在一些帮助下找到了答案。下面的代码将允许您使用上面的代码进行递归搜索。希望这会有所帮助。

    $themedir = 'themes/Evolution';
    $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($themedir));
    $cssFiles = new RegexIterator($allFiles, '/\.css$/i');
    
    if(isset($_REQUEST['resetcolor'])){
    $textboxdefaultcolorcode=$_POST['defaultcolor']; //added line
    
    foreach($cssFiles as $cssFile=>$val){
    	$str=file_get_contents($val);
    			$str=str_replace($inicurrentcolor, $textboxdefaultcolorcode, $str); 
    		file_put_contents($val, $str);
    	}
    }

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 2010-11-30
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多