在php代码的输出结果中搜索并替换

时间:2012-10-08 19:25:00

标签: php

我在PHP文件中创建了以下代码以列出目录的内容 它工作得很好

<?php

  $dir = opendir(getcwd());
  while (($file = readdir($dir)) !== false) {
    echo "filename : ". $file ."<br />";
  }
  closedir($dir);

?>

我想在输出(字符串)结果中进行一些更改。我尝试过搜索并替换PHP代码str_replace(),但我无法让它工作。

2 个答案:

答案 0 :(得分:1)

您可以尝试以下

$dir = opendir(getcwd());
$max = 3 ;
$current = 0 ;
print('<table id="hor-minimalist-a" summary="Employee Pay Sheet"><tbody>');
while ( ($file = readdir($dir)) !== false ) {
    if ($file == "." || $file == "..") {
        continue;
    }
    $current++;
    if($current > $max)
        print("<tr>");
    printf("<td>%s</td>",$file);
    if($current >= $max)
    {
        print("</tr>");
        $current = 0;
    }
}
closedir($dir);
print('</tbody></table>');

答案 1 :(得分:0)

试用此代码

您可以在代码中使用if(!is_dir($file))条件     

  $dir = opendir(getcwd());
  while (($file = readdir($dir)) !== false) {

    if(!is_dir($file))
      {
            echo "filename : ". $file ."<br />";
    }       
  }
  closedir($dir);

?>
相关问题