php 从目录创建一个 css 样式表列表

标签 php html css

我试图从目录中读取 css 文件并创建相应的 html 标记,例如

<link rel="stylesheet" type="text/css" media="screen" href="css/bootstrap.css"> 

PHP

 $css_dir = 'application/resources/css';

$fp = opendir($css_dir);
while ($file = readdir($fp)) 
{
if (strpos($file, '.css',1))
$results[] = $this->base.'/'.$file;                 
}
closedir($fp);

print_r($results);

这可以正确地创建一个数组,其结果类似于 [1] => Directory/name.css 这很好,但是如果我将结果行更改为类似

$results[] = '<link rel="stylesheet" type="text/css" media="screen" href="'.$this->base.'/'.$file.'">';

我得到的是空数组,问题在于“<”“>”符号是否没有办法一次性完成此操作而不必通过结果数组执行 foreach?

最佳答案

而不是使用 <>使用 &lt;&gt;因此,或 htmlspecialchars()

功能代码的改进

 /* This is the WRONG way to loop over the directory. */
    while ($entry = readdir($handle)) {
        echo "$entry\n";
    }

 /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

Reference

关于php 从目录创建一个 css 样式表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819086/

相关文章:

php - Laravel Eloquent 计数乘法

php - Internet Explorer z-index 不适用于打开的 Flash 图表

php - 左外连接的问题

html - 未能使投资组合响应

php - 如何将用户对象绑定(bind)到中间件中的请求

javascript - 更改导航项的颜色(下拉 - 单击)

php - 多图像选择并使用 jquery 打印选定的图像

css - 如何在css中放置链接?

html - 如何在全尺寸超大屏幕中使用 bootstrap 4 将一些文本居中,并仅为 div 或 p 容器设置最大宽度?

html - 文本裁剪到中心