I have is an array of markdown files like this:
$mdfiles = glob("content/*.txt", GLOB_NOSORT);
I want to sort the files by a certain line inside each of them.
An example file is:
File
====
line-one:
date: [number-to-sort]
The array of files is then sorted by [number-to-sort] in each file, which can be accessed by:
$file_array = file($mdfiles[*], FILE_IGNORE_NEW_LINES)
substr($file_array[*], 6);
Finally I would like to strip each content/ and .md from the array key values.
The code looked a lot smaller in my mind, but the resulting code is just three lines 🙂
The
'by_file_date'function is declared later and basically uses theget_datefunction internally to perform the date “pulling” from the files. I’ve usedpreg_matchbased on the form you’ve shown to find the date value; I’ve assumed thatdateis an integer (i.e. sequence of digits). If not, let me know.Finally, you need to strip the filename; Assuming you just want the file name and not the directory:
Not sure where
.mdcame from, so you have to let me know on that one 🙂