douhuiwan5141 2016-10-01 00:43
浏览 29
已采纳

使用PHP在文件夹中搜索CSS并更改css值

I'm new to php and need some help.

I have a text box that a user can type in an html color code (ex:#373737). Once they hit the submit button I want find all color codes matching #379BB9 and replacing them with the new color code they typed in the text box. I want to change this entry on all the css files in a folder. What i'm looking for is the php code to make this work. I've found bits and pieces of code but I could make it work. I only put the code here that relates to the button. Thanks for any help you can give.

Here's the original color code: #379BB9

p {
    background-color:#373737;
    border:1px solid #373737;
    padding-top:5px;
    padding-bottom:5px;
    padding-left:10px;
    padding-right:10px;
    color:#379BB9;
    width:60px;
    cursor:pointer;
    -moz-border-radius:3px;
    border-radius:3px;
    text-transform:uppercase;
} 

.message {
    background-color:#4f4f4f;
    border:1px solid #373737;
    width:98.5%;
    text-align:center;
    margin-bottom:30px;
    -moz-border-radius:3px;
    border-radius:3px;
    text-transform:none;
    color:#379BB9;  
}

.steps {
    text-transform:none;
    color:#379BB9;
}

div.notinstalled {
        color:#f20723;
}

div.installed {
        color:#2cb517;
}

#content a:link, #content a:visited {
    color:#379BB9;
    font-weight:bold;
    text-decoration:none;
}
<h2>My example.php</h2>
<?php
Need php here........
?>
<label id="steps">Here you can set the theme color.</label>
<br>
<Form name="default1" method="POST" action="example.php">
<label for="defaultcolor">Theme Color: </label><input style="color:#ffffff" type="text" id="defaultcolor" name="defaultcolor" value="Add color code here">
<br>
<input type="submit" name="setcolor" value="Set Theme Color">
</form>

</div>
  • 写回答

1条回答 默认 最新

  • down_load1117 2016-10-01 02:32
    关注

    Here the code:-

    if(isset($_REQUEST['setcolor'])){
    $arr=glob("css\*.css"); //your css file's path
    $colorcode=$_POST['defaultcolor'];    
    foreach($arr as $key=>$val){
    $str=file_get_contents($val);
    //$str=preg_replace('/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/',$colorcode, $str); //for replace all colour code
    $str=str_replace("#379BB9", $colorcode,$str ); // for replace only desired colour code
    file_put_contents($val, $str);
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分