Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使数组的每一行都成为一个单独的字符串_Php_Arrays_String_Implode - Fatal编程技术网

Php 使数组的每一行都成为一个单独的字符串

Php 使数组的每一行都成为一个单独的字符串,php,arrays,string,implode,Php,Arrays,String,Implode,我正在努力使扫描目录时,只有图片文件显示为图片,而不是链接。以下是我用来查找文件的内容: $images=glob(getcwd().'/*{jpeg,gif,png}', GLOB_BRACE); $pattern=implode("<br>", $images)."<br>"; 如何将这些行中的每一行都称为字符串 以下是我的全部代码,但这些代码并不适用于我: <?php function showImage() { $filelist = glob(getc

我正在努力使扫描目录时,只有图片文件显示为图片,而不是链接。以下是我用来查找文件的内容:

$images=glob(getcwd().'/*{jpeg,gif,png}', GLOB_BRACE);
$pattern=implode("<br>", $images)."<br>";
如何将这些行中的每一行都称为字符串

以下是我的全部代码,但这些代码并不适用于我:

<?php

function showImage() {
$filelist = glob(getcwd()."/*");
$path= getcwd();
$array = explode("/", $path);
$filename=implode("/", array_slice($array, 4));
$user=implode("/", array_slice($array, 2, 1));
$images=glob(getcwd().'/*{jpeg,gif,png}', GLOB_BRACE);
$pattern=implode("<br>", $images)."<br>";

echo implode("<br>", $images);

if ($filelist != false) {
print "<p>Here are the folders and files in".getcwd().":</p>";
foreach ($filelist as $file) {
  if(ereg($pattern, $file)) {
  $url = "http://hills.ccsf.edu/~".$user."/".$filename."/" . substr($file, strrpos($file, '/') + 1);
  print "<a href=".$url."><img src=".$url." height='100' width='100'></a><br><br>";
       }
  if(!ereg($pattern, $file)) {
  $url = "http://hills.ccsf.edu/~".$user."/".$filename."/" . substr($file, strrpos($file, '/') + 1);
  print "<a href=".$url.">".$url."</a><br><br>";        
    }
}
} else {
print "<a href=".$url.">".$url."</a><br><br>";
}
}

showImage();

?>
我尝试使用: !feofifereg$pattern$file


但这不是正确的使用!feof会显示图片,但随后会发出大量其他警告。

您希望显示指向所有文件的链接,但对于图像,您也希望显示图像。事实上,您仍然希望链接到所有文件,这似乎是您问题描述中缺少的信息

如前所述,在注释中,一个快速修复方法就是更改ifereg$pattern,$file{with if in_array$file,$images{。并将第二部分括在else块中,不要使用另一个if并否定表达式!换句话说

if (in_array($file,$images)) {
    /* Link and display image */
} else {
    /* Just link the file */
}
或者,您也可以在遍历$filelist时检查每个文件是否为图像,而不是在循环之前执行此操作,并将这些文件存储在另一个名为$images的数组中,然后需要查找该数组。并通过将$url分配移到if块之外来避免代码重复。例如:

$url = '<Construct URL before IF block>';
if (preg_match('/\.(jpe?g|gif|png)$/i',$file)) {
    /* Link and display image */
} else {
    /* Just link the file */
}
$html = '';
foreach ($filelist as $file) {
    /* ... */
    $html .= '<Some HTML>';
    /* ... */
    $html .= '<Some more HTML>';
}
echo $html;

如果您绝对需要单个字符串变量而不是字符串数组,那么Huii,您的目标是什么?您的输出和预期输出是什么?目标是扫描目录中的文件,并将所有图像文件显示为小图像,这些图像文件是链接,所有非图像文件都显示为URL。此链接显示我的代码的输出,我想要这两个要以图片形式显示的图像文件。@MarkBaker我现在正在研究提取,谢谢!但是为什么不能简单地迭代返回的数组,它看起来比需要的复杂得多
$html = '';
foreach ($filelist as $file) {
    /* ... */
    $html .= '<Some HTML>';
    /* ... */
    $html .= '<Some more HTML>';
}
echo $html;