我试图从目录中读取 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?
最佳答案
而不是使用 <
和 >
使用 <
和 >
因此,或 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";
}
关于php 从目录创建一个 css 样式表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819086/