- basename: documentation ( source)
- function_exists: documentation ( source)
- strrpos: documentation ( source)
- printf: documentation ( source)
- str_ends_with: documentation ( source)
<?php
if (!function_exists("str_ends_with")) {
function str_ends_with($haystack, $needle) {
return strrpos($haystack, $needle) === strlen($haystack) - strlen($needle);
}
}
$urls = [
"https://www.example.gov.us/directory_1/directory_2/directory_3/index.php",
"https://www.example.gov.us/directory_1/directory_2/directory_3/index",
"https://www.example.gov.us/directory_1/directory_2/directory_3/"
];
foreach ($urls as $url) {
printf(
"From %s, got '%s'%s",
$url,
str_ends_with($url, "/") ? "" : basename($url),
PHP_EOL
);
}