Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File() acting weird on CSS file 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys :)

I'm encountering something very weird :

file("style.css") returns an empty array if I leave
Code:
<style type="text/css">
<!--
and
Code:
-->
</style>
in it! :(

Why is that?
 
Maybe there is a misunderstanding ;)

When I leave my css file in its original state, file("style.css") returns an empty array (weird behaviour).

If I manually remove the css tags in the file, file("style.css") returns the css content as an array (normal behaviour).

 
are you sure it is an empty array and not a value of FALSE?

you can check with the === operator

 
Thanks guys :)

Well, I'm afraid the problem will be left unsolved because I've chosen to avoid the issue by using something else than file() simply because I didn't want to waste more time on it.

As for Jpadie's question, yes the array existed for real but was empty.
 
Sleidia

can you post a sample of your styles file (as caused problems) together with a brief view of your setup (OS and versions). if what you report can be replicated then it is a bug that should be reported to PHP (which I am happy to sort out).

thanks
 


style_with_tags.css :

Code:
<style type="text/css">
<!--

/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */

/* GLOBAL : DEFAULT */
/* _________________________________________________________________________ */
/* _________________________________________________________________________ */

body {

background-color: #ffffff;
margin: 0px;

}

body, input, textarea, select {

font-family: Arial, Verdana, Helvetica, sans-serif;
font-size: 0.9em;

}


/* ----------------------------------------------------------------------- */

-->
</style>

style_without_tags.css :

Code:
/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */

/* GLOBAL : DEFAULT */
/* _________________________________________________________________________ */
/* _________________________________________________________________________ */

body {

background-color: #ffffff;
margin: 0px;

}

body, input, textarea, select {

font-family: Arial, Verdana, Helvetica, sans-serif;
font-size: 0.9em;

}


/* ----------------------------------------------------------------------- */

PHP :

Code:
<?php

$css_array = file("style_with_tags.css");

echo nl2br(print_r($css_array, true));

/*

returns

Array
(
[0] =>

)

*/

echo "<br /><br /><br /><br />";

$css_array = file("style_without_tags.css");

echo nl2br(print_r($css_array, true));

?>


 
echo nl2br(print_r($css_array, true));

that's why it does not work.

the file has the <style> tags embedded and so the browser knows not to display it on the screen.

if you want to view html on the screen as html (rather than as rendered html) you need first to encode it with htmlspecialchars or htmlentities.

when you take the <style> tags out the browser thinks it is just plain display text it is getting so renders it for you.

the html comment tags will have a similar net effect.

so ... to display the css you need to do this:

Code:
echo "<pre>\r\n";
foreach($css_array as $line){
 echo htmlspecialchars($line);
}
echo "</pre>";

[/code]
 
that's strange, when using Sleidia's code, I get the behaviour that he describes, which is what i'd also expect given the the code.

of course with the code I posted, both sets of css should display ok.

and as an aside, external css files should not have <style> tags but i assumed that Sleidia is including() the files in a page rather than using <style href=""> tags or the @import method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top