personal-website/main.php

28 lines
908 B
PHP
Raw Permalink Normal View History

2021-11-28 12:48:45 +00:00
<?php
2023-01-10 14:54:53 +00:00
// Global variables
$_dr = $_SERVER['DOCUMENT_ROOT'];
// Parse path into array
// (e.g."/hello////13/thing" into ["hello", "13", "thing"])
$GLOBALS['PARSED_PATH'] = (function () {
/* - Usage of '#' as delimiters to be able to use '/' without escaping
* - (a|b) checks patterns a or b and captures it, (?:a|b) doesn't capture
*
* - (?:/|) is there to remove the first '/'
* - ([^/]+?) gets the content in a non-greedy way
* - (?:/|$) limits the capture to the next '/' or the end of the string
*/
preg_match_all('#(?:/|)([^/]+?)(?:/|$)#', $_SERVER['DOCUMENT_URI'], $matches);
// If it doesn't find anything, it is the root
$matches[1][0] = $matches[1][0] ?? '';
// We don't care about the full pattern, just the captured group
return $matches[1];
})();
match ($GLOBALS['PARSED_PATH'][0]) {
'' => include_once "$_dr/pages/index.php",
default => printf("404"),
};