【问题标题】:PHP - read file and echo resultsPHP - 读取文件并回显结果
【发布时间】:2011-01-20 13:14:27
【问题描述】:

我有 /fonts/ 文件夹,里面装满了 .js 文件。

我知道如何阅读这个文件夹并列出那里的所有文件:

 $dir = "/fonts"; 
        if (is_dir($dir)) {     
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    echo '<tr><td>'. $file .'</td></tr>';
                }
            closedir($dh);
            }
        }

但我不想写文件名而是他们存储的数据。

里面的图案是这样的:

NameOfTheFontFile_400_font.js:

(...) "font-family":"NameOfTheFont" (...)

那么如何修改我的第一个脚本以打开读取每个文件并获取字体系列名称而不是文件名?

非常感谢!

【问题讨论】:

    标签: php dir opendir


    【解决方案1】:

    您可以使用readfile() 来回显它的输出。另请注意,这未经测试,但应该可以工作:

    $dir = "/fonts"; 
        if (is_dir($dir)) {     
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    echo '<tr><td>';
                    readfile($file);
                    echo '</td></tr>';
                }
            closedir($dh);
            }
        }
    

    如果您的 .js 文件在字体名称旁边有额外的数据,您可以执行以下操作来查找文件名:

    $dir = "/fonts"; 
        if (is_dir($dir)) {     
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    $lines = file($file);   
                    foreach ($lines as $line) {
                        if (preg_match('/("font-family":)(".+")/', $line, $parts) ) {                   
                            echo '<tr><td>', $parts[2], '</td></tr>';   
                        }
                    }
                }
            closedir($dh);
            }
        }
    

    题外话:为什么将字体名称存储在 .js 文件中?最好将它们存储在 xml 文件或数据库中,因为这就是它们的用途。

    【讨论】:

      【解决方案2】:

      来自php manual

      $lines = file($file);
      

      编辑:这也许可以优化,但要获得与字体的行:

      foreach ($lines as $line)
      {
        if (strpos($line, 'font-family') !== false)
        {
          echo $line;
        }
      }
      

      您可以使用字符串函数或正则表达式进一步挖掘该行以获得确切的字体名称(例如使用strpos()),但如何做到这一点取决于文件的一般格式。

      【讨论】:

      • 我知道如何打开文件,但不知道如何获取准确的单词。
      • @anonymous 我已经添加了更多细节以获得正确的线路。
      【解决方案3】:

      这样就可以了:

      $dir = '/fonts';
      
      $files = array_filter(glob("$dir/*"), 'is_file');
      $contents = array_map('file_get_contents', $files);
      foreach ($contents as $content) {
              if (preg_match('#"font-family":"([^"]+)"#', $content, $matches)) {
                      echo '<tr><td>'.htmlspecialchars($matches[1]).'</td></tr>';
              }
      }
      

      或者换一种风格:

      $files = glob("$dir/*");
      foreach($files as $file) {
              if (is_file($file)) {
                      $content = file_get_contents($file);
                      if (preg_match('#"font-family":"([^"]+)"#', $content, $matches)) {
                              echo '<tr><td>'.htmlspecialchars($matches[1]).'</td></tr>';
                      }
              }
      }
      

      【讨论】:

        【解决方案4】:

        从文档 - http://www.php.net/manual/en/function.file-get-contents.php 中,您可以使用 file_get_contents 使用从目录列表中获得的文件名来获取文件的内容。

        string file_get_contents ( string$filename [, bool$use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )

        注意:其他人已经详细回答了这个问题。编辑此答案以响应自私鱼的评论以详细说明链接的文档。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-28
          • 2013-08-11
          • 1970-01-01
          相关资源
          最近更新 更多