1. 程式人生 > >php獲取資料夾中所有lrc格式檔案,提取lrc檔案中內容

php獲取資料夾中所有lrc格式檔案,提取lrc檔案中內容

  • 功能說明:
    一個資料夾內有MP3檔案和lrc(歌詞檔案),我要提取資料夾下所有的lrc檔案內容中的標題.
    一個lrc檔案的內容像這樣:
    [al:新概念英語(一)]
    [ar:MP3 同步字幕版(美音)]
    [ti:Sorry, Sir.]
    [00:00.79]Lesson 3
    [00:02.98]Sorry sir.
    [00:05.75]Listen to the tape then answer this question.
    [00:11.71]Does the man get his umbrella back?
    [00:17.56]My coat and my umbrella please.
    [00:22.00]Here is my ticket.
    [00:25.03]Thank you sir.
    [00:26.86]Number five.
    [00:29.09]Here’s your umbrella and your coat.
    [00:33.72]This is not my umbrella.
    [00:37.39]Sorry sir.
    [00:39.67]Is this your umbrella?
    [00:42.56]No it isn’t.
    [00:45.69]Is this it?
    [00:47.76]Yes it is.
    [00:50.53]Thank you very much.

  • 提取[ti:Sorry, Sir.]中的Sorry, Sir.

  • 提取所有的lrc檔案中的ti: , 輸出一個列表.

1. 使用scandir()方法可以獲取資料夾中檔名

2. 遍歷提取字尾名為.lrc的檔名

3. 讀取lrc內容

4. 提取內容中的[ti:]標題輸出

5. 建立超連結

<?php 

/*獲取資料夾中所有檔名稱*/
$dir = "./lesson/";
$files = scandir($dir);

//除錯列印
// echo '<pre>';
// print_r($files);
// echo '<pre>';

foreach
($files as $key => $value) { //直接使用正則檢測檔案字尾名,取lrc型別 if (preg_match('/\.lrc/i', $value)){ //$dir.$value為檔案url,開啟檔案通道 $path = fopen($dir.$value,'r'); // 開啟檔案,fread()讀取其內容賦給\$lrc $lrc = fread($path,filesize($dir.$value)); /*提取標題: * preg_match(正則表示式\$reg,處理\$lrc,處理後存入$temp_title) * 正則檢測[ti:]和其後的字串存入$temp_title * 如果有匹配此表示式為true */
$reg = '/(?:\[ti\:)(.*)(?:\])/i'; if (preg_match($reg ,$lrc, $temp_title)) { // 去掉[ti:],將[ti:]內的字串存為標題title // 檔名稱 $filename = $value; //只要內容不要[ti:],取[1] $title = $temp_title[1]; // 生成超連結 $title_link = '<li><a href="'.$dir.$filename.'">'.$title.'</a></li>'; echo $title_link; // array_push($file_list, $title_link); } fclose($path); } } ?>
  • 我得到的列表:
    這裡寫圖片描述

  • 點選連結得到檔案(歌詞)內容
    這裡寫圖片描述

劣勢:
1. 當lrc檔案中沒有標題,那這個檔案就列不了了
2. 這裡我以後綴名來判斷檔案格式, 你有沒有其他的方法呢? 歡迎指教

相關推薦

php獲取資料所有lrc格式檔案,提取lrc檔案內容

功能說明: 一個資料夾內有MP3檔案和lrc(歌詞檔案),我要提取資料夾下所有的lrc檔案內容中的標題. 一個lrc檔案的內容像這樣: [al:新概念英語(一)] [ar:MP3 同步字幕版(美音)] [ti:Sorry, Sir.] [00:00

獲取資料所有圖片名字,用 glob模組,簡單操作就可以,不用os的操作 python的一個好用的檔名操作模組glob

轉 python中的一個好用的檔名操作模組glob 2017年09月19日 16:48:13 何雷 閱讀數:1963

C語言獲取資料所有檔名並列印到txt

程式如下: /********************************************************************************************

boost/system獲取資料所有檔名稱

需要包含#include <boost/filesystem.hpp> BOOL GetAllFiles(vector<string>& vecFiles, string iPath, BOOL bRecursive) {     b

Matlab獲取資料所有檔名並將資料按矩陣賦值給變數

fileFolder=fullfile('D:\MATLAB\bin\trc'); dirOutput=dir(fullfile(fileFolder,'*.trc')); fileNames={dirOutput.name}; n = 1; for i=fileNames

php複製檔案PHP複製資料下面所有檔案

說明:下面的程式碼實現了將d盤test1資料夾裡的全部內容複製到d盤的test2資料夾中 <span style="font-size:18px;"><?php function recurse_copy($src,$des) { $dir = o

C/C++獲取資料所有檔案的名字及全路徑

在實際中,會遇到需要通過程式獲取當前路徑下所有檔案的名稱,以下函式可以實現獲取所給路徑下檔名 及檔案對應的全路徑;需要包含文章末尾的兩個標頭檔案#include <vector> #include"dirent.h" #include"unistd.h" usi

C++遍歷獲取資料下面所有檔名

#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #ifdef linux #incl

c++ 獲取資料所有檔案

用c++獲取指定資料夾下的所有檔案,包括子資料夾裡的檔案。 void GetAllFiles( string path, list<string>&AllFiles, bool

C/C++ 獲取資料所有檔名 windows和linux通用

參考: ################################################# 利用C/C++編寫程式以獲取資料夾內所有子檔名,以下程式參考網路上諸多博文: 標頭檔案如下: #include <iostream> #inclu

Unity編輯器獲取資料所有物件

有一次需要在Editor模式下獲取任意資料夾下的所有資源物件,包括子資料夾,我嘗試了AssetDatabase.LoadAllAssetsAtPath(string assetPath),理想中的Object[]沒有返回,而是給我返回了null;查閱google得知這個方法

C++獲取資料所有檔名稱的三種方式

1 利用dos命令把資料夾下所有檔名存入指定檔案,再從檔案讀取,存入vector中 string imglist_file = "test_result\\imglist.txt";//儲存檔名稱列表 string temp_imgname; stri

MFC獲取資料所有檔案的名字ListBox

臨時用到,就寫了一下,儲存一下成果! 基於List Box的介面顯示檔名列表: 執行效果圖: 具體實現主要是新增兩個訊息響應,一個BUTTON控制元件的,一個是ListBox控制元件的LBS_SELCHANGE訊息訊息響應; 程式碼很簡單,如下: void CAddMt

Java 獲取資料所有檔案

/** * 獲取路徑下的所有檔案/資料夾 * @param directoryPath 需要遍歷的資料夾路徑 * @param isAddDirectory 是否將

MFC下使用CFileFind獲取資料所有檔案

void AddFileFromFolder(const CString strFolderPath) { CString strMatch = strFolderPath + _T("\\*.*"); CString strFullName; CFileFind f

OpenCV獲取資料所有檔名

一、OpenCV中有實現遍歷資料夾下所有檔案的類Directory,它裡面包括3個成員函式:(1)、GetListFiles:遍歷指定資料夾下的所有檔案,不包括指定資料夾內的資料夾;(2)、GetListFolders:遍歷指定資料夾下的所有資料夾,不包括指定資料夾下的檔

【python】儲存某個資料所有圖片名字到一個txt檔案

用python實現讀取某一資料夾下的所有圖片名字到一個txt檔案 import os dir1='/data/Datasets/JPEGImages'#圖片檔案存放地址 txt1 = '/data/Datasets/picture.txt'#圖片檔名存放txt檔案地址 f1 = open

使用C++獲取資料所有檔名(windows環境)

由於經常有讀取一個資料夾中的很多隨機編號的檔案,很多時候需要讀取某些特定格式的所有檔案。 下面的程式碼可以讀取指定檔案家中的所有檔案和資料夾中格式為jpg的檔案 windows平臺程式碼:#if 1 #include <io.h> #include

nodejs 獲取資料檔案獲取資料所有圖片

//引用檔案系統模組 var fs = require("fs"); //引用imageinfo模組 var image = require("imageinfo"); function readFi

利用python.os.listdir獲取資料所有檔名

利用內建os的listdir可以獲取資料夾中的檔名,從而可以利用檔名對資料做進一步的處理,比如:在貓狗識別的練習過程中,需要通過檔名對貓狗資料進行分類。 def get_file(file_dir): dogs = [] cats = []