我需要根据前端更改的变量创建一个可下载的 css 文件。
注意:这是缩短的示例。
CSS:
.color: red; width: incrementvalue();
在 html id 中有一个 inout 字段来调整宽度,示例:
HTML
<input type="text" value="3">
然后我有一个函数,该函数将在输入发生变化时运行,该函数将执行以下操作:
$('input').on ('change', function (){return $(this).val () * 2});
是的,上面的函数是在 jquery 中,但我真的不知道 php,我告诉我这应该在 php 中完成。
有什么想法吗?
最佳答案
您可以创建一个以 CSS header 开头的常规 PHP 文件:
<?php
header('Content-type: text/css');
之后,您可以只回显 CSS 内容。一种方法是使用带有模板系统的 heredoc 语法。该模板将变量值放在“%%”百分比符号内作为分隔符。这是一个例子:
$css = <<<EOTAAA
* {
margin: 0;
padding: 0;
}
body {
background-color: %bg_color%;
background-image: url(images/bg.gif);
background-repeat: repeat-x;
font: 14px Arial, Helvetica, sans-serif;
}
div.my_div{
color: red;
width: %width%;
}
EOTAAA;
$search = array('%bg_color%', '%width%');
$width = width_function();//some function for determining width
$bg_color = bg_function();//some function for determining background color
$replace = array($bg_color, $width);
echo str_replace($search, $replace, $css);
下载链接:
在您的 HTML 中,使用将处理请求的文件的 URL 创建一个类似这样的链接:
<a href="HTTP://www.example.com/download.php">Get CSS</a>
然后在您的 PHP 文件中,将 header 信息更改为以下内容:
header("Content-Disposition: attachment; filename=\"custom.css\"");
header('Content-type: text/css');
header("Content-Type: application/force-download");
header("Connection: close");
关于php - 用php创建css文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17464585/