CSS update instances implemented by php and phpcss update instances

Source: Internet
Author: User
Tags learn php programming

CSS update instances implemented by php and phpcss update instances

This example describes the CSS update class implemented by php and its usage, which is very practical. Share it with you for your reference. The details are as follows:

The CSSUpdate. class. php class file is as follows:

<? Php/** css update class: update the image version in the css file * Date: 2013-02-05 * Author: fdipzone * Ver: 1.1 ** Func: * update (); ** Ver: 1.1 added the search_child parameter to traverse subfolders */class CSSUpdate {private $ cs1_pl_path = null; private $ css_path = null; private $ replacetags = array (); private $ search_child = false; private $ convert_num = 0; private $ is_ready = 0;/** initialize * @ param String $ cs1_pl_path css template path * @ param String $ css_path cs S target path * @ param Array $ replacetags indicates the image type to be replaced * @ param boolean $ whether search_child traverses subfolders. The default value is false */public function _ construct ($ cs1_pl_path, $ css_path, $ replacetags = array (), $ search_child = false) {if (! Is_dir ($ cs1_pl_path) |! Is_dir ($ css_path) |! $ Replacetags) {$ this-> is_ready = 0;} else {$ this-> cs1_pl_path = $ cs1_pl_path; $ this-> css_path = $ css_path; $ this-> replacetags = $ replacetags; $ this-> search_child = $ search_child; $ this-> is_ready = 1 ;}} /** update the css file */public function update () {if ($ this-> is_ready = 0) {$ this-> response ('csstmpl or csspath or replacetags error'); return '';} $ this-> traversing ($ this-> cs1_pl_path ); $ this-> response (' Covert num :'. $ this-> convert_num);}/** traverse the folder * @ param String $ path file path */private function traversing ($ path) {$ handle = opendir ($ path ); while ($ file = readdir ($ handle ))! = False) {if ($ file! = '..' & $ File! = '. ') {$ Curfile = $ path. '/'. $ file; if (is_dir ($ curfile) {// folder if ($ this-> search_child) {// You Need to traverse the subfolders $ this-> traversing ($ curfile) ;}} elseif ($ this-> checkExt ($ curfile )) {// css file $ dfile = str_replace ($ this-> csstmpl_path, $ this-> css_path, $ curfile); $ this-> create ($ curfile, $ dfile ); $ this-> response ($ curfile. 'convert '. $ dfile. 'success'); $ this-> convert_num ++; }}} closedir ($ handle);}/** check Query the file suffix */private function checkExt ($ file) {$ name = basename ($ file); $ namefrag = explode ('. ', $ name); if (count ($ namefrag)> = 2) {if (strtolower ($ namefrag [count ($ namefrag)-1]) = 'css ') {// return true;} return false;}/** Replace the template content, write csspath * @ param String $ tmplfile template file * @ param String $ dfile target file */private function create ($ tmplfile, $ dfile) {$ css_content = file_get_contents ($ tmplfile ); foreac H ($ this-> replacetags as $ tag) {$ css_content = str_replace ($ tag, $ tag ."? ". Date ('ymdhis '), $ css_content);} if (! Is_dir (dirname ($ dfile) {// generate the target path mkdir (dirname ($ dfile), 0755, true);} file_put_contents ($ dfile, $ css_content, true);}/** output */private function response ($ content) {echo $ content. "<br>" ;}}?>

The demo sample program is as follows:

<?php require_once "CSSUpdate.class.php";  define('ROOT_PATH', dirname(__FILE__)); $css_path = ROOT_PATH.'/css'; $csstmpl_path = ROOT_PATH.'/csstmpl'; $replacetags = array('.png', '.jpg', '.gif');  $cssobj = new CSSUpdate($csstmpl_path, $css_path, $replacetags); $cssobj->update(); ?>

Click here to download the complete source code.

I hope this article will help you learn PHP programming.


PHP uses css to change the display content. If the answer is correct, add 50.

I used javascript to complete
============== First.html ================

<Html>
<Head>
<Title> first.html </title>
</Head>
<Body>
<Form action = "second.html" method = "get" name = "myform">
<Input name = "csstype" type = "text"/>
<Input name = "submit" type = "submit"/>
</Form>
</Body>
</Html>

============= Second.html ===================================

<Html>
<Head>
<Title> second.html </title>
<Style type = "text/css">
. Type1 h1 {
Color: # f00;
}
. Type1 p {
Color: #0f0;
}
. Type2 h1 {
Color: # ffff00;
}
. Type2 p {
Color: #00 ffff;
}
. Type3 h1 {
Color: # ff00ff;
}
. Type3 p {
Color: # ff00ff;
}
</Style>
</Head>
<Body>
<Div id = "wrap">
<Script type = "text/javascript" language = "javascript">
Function QueryString (item ){
Var sValue = location. search. match (new RegExp ("[\? \ &] "+ Item +" = ([^ \ &] *) (\ &?) "," I "))
Return sValue? SValue [1]: sValue
}
Var csstype = QueryString ('csstype ');
Var wrap = document. getElementById ("wrap ")
Switch (csstype)
{
Case "2 ":
Wrap. className = "type2 ";
Break;
Case "3 ":
Wrap. className = "type3 ";
Break;
Default:
Wrap. className = "type1 ";
}
</Script>
<H1> title <P> body text & lt ...... remaining full text>

How can php dynamically obtain data in css, such as dynamically changing the width and height?

This should be obtained from html pages using javascript. How do you use the server language to obtain the css of html pages running in browsers.
<Div id = "test" style = "background-color: red;"> </div>
<Script type = "text/javascript">
Document. write (document. getElementById ("# test"). style. backgroundColor );
</Script>
It should be obtained like this. If your server requires such information, use ajax or forms to return the information to the php page of the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.