{"id":3012,"date":"2024-10-23T17:40:53","date_gmt":"2024-10-23T15:40:53","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3012"},"modified":"2024-12-04T18:13:12","modified_gmt":"2024-12-04T17:13:12","slug":"php-list-files-and-metadata","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3012","title":{"rendered":"php list files and metadata"},"content":{"rendered":"\n<p>This is a good code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho \"&lt;ul class=\\\"flexmenu\\\"&gt;\\n\";\n$phpfiles = glob(\"$path.\/&#91;^index]*.php\"); \/\/ Get all PHP files except index.php\nforeach ($phpfiles as $phpfile) {\n    $contents = file_get_contents($phpfile);\n\n    preg_match('\/\\$title\\s*=\\s*\"(.*?)\";\/', $contents, $titleMatch);\n    preg_match('\/\\$subtitle\\s*=\\s*\"(.*?)\";\/', $contents, $subtitleMatch);\n    preg_match('\/\\$description\\s*=\\s*\"(.*?)\";\/', $contents, $descriptionMatch);\n    preg_match('\/\\$order\\s*=\\s*\"(.*?)\";\/', $contents, $orderMatch);\n\n    $title = $titleMatch&#91;1] ?? 'No title';\n    $subtitle = $subtitleMatch&#91;1] ?? 'No subtitle';\n    $order = $orderMatch&#91;1] ?? '';\n    $description = $descriptionMatch&#91;1] ?? 'No description';\n\n    \/\/ Correctly extract the filename\n     $filename = $phpfile; \/\/ Use basename on $phpfile directly\n    echo \" &lt;li role='menuitem' style=\\\"order:$order\\\"&gt;&lt;a href=\\\"{$filename}\\\"&gt;{$title}&lt;\/a&gt;, &lt;i&gt;{$subtitle}&lt;\/i&gt;: {$description}&lt;\/li&gt;\\n\";\n}\necho \"&lt;\/ul&gt;\";\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>You can get the title of your files in that folder (useful if the name of the file would not suit for a list), the subtitle and description.<br>With the order variable you can set, in a <strong>flex<\/strong> list (and this is why is added a .flexmenu class: a no-flex list doesn&#8217;t order your items as expected, but alphabetically), in what order your files should appear.<br>Of course the title is linked with the matching file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a good code: You can get the title of your files in that folder (useful if the name of the file would not suit for a list), the subtitle and description.With the order variable you can set, in a flex list (and this is why is added a .flexmenu class: a no-flex list &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=3012\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;php list files and metadata&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[742,751,566],"tags":[903,902,178,904],"class_list":["post-3012","post","type-post","status-publish","format-standard","hentry","category-css-webmaster","category-php","category-webmaster","tag-automatic-list","tag-list","tag-metadata","tag-metatags"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3012"}],"version-history":[{"count":3,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions"}],"predecessor-version":[{"id":3071,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions\/3071"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}