user007
user007

Reputation: 3243

PHP - include multiple css files function

I am trying to make a function which will load my css file easily. I dont have enough knowledge of PHP, so please help me:

I want to call that function as:

<?php load_css('reset.css,main.css,bootstrap.css'); ?>

Please tell me how do i separate all file name from function parameter and call them one by one. My current function:

<?php

load_css($files){

echo '<style src="'.$files.'"></style>';

}

?>

Upvotes: 0

Views: 2456

Answers (5)

mohammad mohsenipur
mohammad mohsenipur

Reputation: 3149

try this that combine and minify your css and you need only add one css instead several css file on page

css.php

<?php 
$now=time()+10000;
$then="Expires: ".gmstrftime("%a,%d %b %Y %H:%M:%S GMT",$now);
header($then); 
header("Cache-Control:  public, must-revalidate"); 
header("Content-Type: text/css");
ob_start("ob_gzhandler");
set_time_limit(0);
//list of your css
$CssList=array('main.css',
'simple-lists.css');
$outt='';
foreach($CssList as $CSS){
   $outt.=minify_css($CSS);
}
function minify_css($add){
   $fp=fopen($add,'rb');
   $speed=1024*100;
   while(!feof($fp)){
     $out.=fread($fp,$speed);
   }
   $out = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $out);
   /* remove tabs, spaces, newlines, etc. */
   $out = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $out);
   //$out=str_replace("  ","",$out);
   //$out=str_replace("  ","",$out);
   return $out;
}
print($outt);
while (@ob_end_flush());
?>

put this on your header

<style type="text/css" src="css.php" ></style>

Upvotes: 1

Class
Class

Reputation: 3160

try this

load_css($files){
    $files = explode(",", $files);
    while(list($css) = each($files){
        echo "<style type='text/css' src='" . $css . "' ></style>";
    }
}
$css = 'css.css,css1.css,css2.css';
load_css($css);

or

load_css($files){
    while(list($css) = each($files){
        echo "<style type='text/css' src='" . $css . "' ></style>";
    }
}
$css = array('css.css','css1.css','css2.css');
load_css($css);

Upvotes: 1

Nathan Zhang
Nathan Zhang

Reputation: 11

Why don't you try using traditional HTML css includes instead?

e.g.:

@import cssfile-number.css

(replace -number with different css filenames or numbers).

Hope this helps!

Upvotes: 1

Jhonathan H.
Jhonathan H.

Reputation: 2713

Another idea here that is short,simple and fast to understand..

// store css file names as array..
$css = array('css1','css2','css3');

// then loop to call them one by one.
foreach($css as $style){

    echo '<style src="'.$style.'"></style>';
}

Upvotes: 2

Mahendra
Mahendra

Reputation: 928

write your functionlike this

<?php

load_css($files){

  $css=explode(",",$file);

  for($i=0;$i<count($css);$i++)
  {
   echo '<style src="'.$css[$i].'"></style>';
  }

}

?>

Upvotes: 1

Related Questions