drogon982007 2011-01-20 13:14
浏览 63

PHP - 读取文件和回显结果

I have /fonts/ folder full of .js files.

I know how to read this folder and list all the files there:

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

But I don't want to write filenames but data they store.

The pattern inside looks like this:

NameOfTheFontFile_400_font.js:

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

So how to modify my first script to open-read each file and grab the font-family name instead of file name?

Thanks a lot!

  • 写回答

4条回答 默认 最新

  • douzi1986 2011-01-20 13:19
    关注

    From the php manual:

    $lines = file($file);
    

    Edit: This can probably be optimized, but to get the line with the font:

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

    You can dig further in that line using string functions or regular expressions to get the exact font name (using for example strpos()), but how to do that depends on the general format of the file.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题