gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:17 25 4
gpt4 key购买 nike

我试图从目录中读取 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com