0

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>';

}

?>
2
  • refer to this question : stackoverflow.com/questions/3472709/… Commented Apr 2, 2013 at 5:57
  • As a side note: remember that spreading your CSS to many files will slow page loading because the browser has to request these files one by one. In the future you may want to look into tools such as assetic that compile many JS or CSS files into one. Assetic is included in the popular Symfony2 framework but can also be used separately. Commented Apr 2, 2013 at 6:06

5 Answers 5

2

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>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think echo '<style src="'.$style.'"></style>'; will be echo '<style src="'.$css.'"></style>';
@user1983017 thats correct im wrong at foreach($style as $cc) it should be foreach($css as $style).. thanks for clarification..
got error Warning: Invalid argument supplied for foreach()
Working function load_js($files){ $css = array($files); foreach($css as $style){ echo '<style src="'.$style.'"></style>'; } }
@user1983017 thats right you can create a function using this preference.. glad it works..
1

write your functionlike this

<?php

load_css($files){

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

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

}

?>

2 Comments

dont forget to accept answer if it works for you. So it will help others too. Happy Coding :)
you have a ( which should be )
1

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!

2 Comments

Forgot to mention that you need to stick that line in a css file on its own, and then use (a) <link rel="stylesheet" href="css-file.css" /> tag(s) to include the css file that imports css files. Confusing, I know :D !!!
This is because he wants it to do with php. comment first to be sure if it a option. but +1 for your orthodox answer. I basically prefer it over all. thanks
1

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);

Comments

1

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>

1 Comment

Man, I was thinking for css minify/compression function. And your code just do that. love it, awesome. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.