【发布时间】:2017-08-11 19:38:40
【问题描述】:
我正在编写一个函数,作为学习练习,用于合并和缩小输出生成的 css 的 css 文件和 php 文件。
在 css 文件中循环一切都很好,但是一旦它尝试从 php 文件中推送从输出缓冲区返回的字符串,数组就会变成一个 int。 var_dump() 产生这个:
int(5)
我也尝试过连接字符串;它再次正常工作,直到它到达 php 文件,然后字符串中前面的所有内容都变为 4。像这样:
4/*
* Home: Appointment Grid
*/
.difAppointmentGrid {
margin: 2rem 0;
font-family: "Lato" !important;
box-shadow: -4px 4px 16px 4px hsla( 240, 0%, 0%, 0.1 );
}
. . .
这是我在 styles.php 文件中所做的示例:
. . .
.difAppointmentGrid header div h3 {
margin: 0;
padding: 1.5rem 0;
font-size: 2rem;
text-align: center;
color: white;
}
<?php
for ( $h3 = 1, $o = 0.40; $h3 <= 4; ++$h3, $o += 0.20 )
{
$rule = '.difAppointmentGrid header div:nth-child('.$h3.') h3 {'."\n\t".
'background-color: hsla( 223, 63%, 22%, '.$o.' );'."\n".
'}'."\n";
echo $rule;
}
?>
.dif_grid {
display: flex;
}
. . .
这是函数:
function styles_init()
{
$path = __DIR__ . '/aggregate.min.css';
if ( is_writable( $path ) )
{
$r = array();
foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
{
$fn = pathinfo( $file );
if ( $fn['extension'] == 'php' || $fn['extension'] == 'css' )
{
ob_start();
include( $file );
$x = ob_get_flush();
$r[] = $x;
}
}
$c = implode( "\n\n", $r );
//$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );
$f = fopen( $path, 'wb' );
fwrite( $f, $c );
fclose( $f );
}
}
最奇怪的部分是在 array_pushing/concatenating 时实际上没有抛出错误。我什至不知道要问什么问题,因为我实际上无法弄清楚出了什么问题。我还搞砸了标头、字符编码、不同的 ob 函数,以及出于绝望将 ob_get_flush 转换为字符串。
解决方案:
function get_include_output($file)
{
ob_start();
include( $file );
return ob_get_flush();
}
function styles_init()
{
$path = __DIR__ . '/aggregate.min.css';
if ( is_writable( $path ) )
{
$r = array();
foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
{
$fn = pathinfo( $file );
if ( $fn['extension'] == 'php' || $fn['extension'] == 'css' )
{
$r[] = get_include_contents( $file );
}
}
$c = implode( "\n\n", $r );
//$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );
$f = fopen( $path, 'wb' );
fwrite( $f, $c );
fclose( $f );
}
}
【问题讨论】:
-
你在哪里倾倒?另外
$r[] = $x比在这里使用array_push()更好。 -
@GentlemanMax 我刚刚用 $r[ count( $r ) ] = $x;它导致了同样的行为。我一直在使用 var_dump() 和 error_log() 来获取循环时的值。我怀疑这是源于我不知道的输出缓冲的某些方面。
-
您不需要
count($r)位,不管怎样,您在array_push()之后执行var_dump($r)?你试过ob_get_clean()吗? -
array_push确实应该返回数组中的新元素数 -
@GentlemanMax 是的,关于两者。我实际上是用 ob_get_clean() 编写的函数,但是我将它切换到 ob_get_flush(),因为后者更可靠,即 ob_get_clean() 只会返回 style.php 的内容,而忽略任何 *.css 文件;而 ob_get_flush() 返回两种文件类型的内容。
标签: php output-buffering