Forum Moderators: coopster
<?php
function lastPartUrl($s, $returnExt = true) {
preg_match("/\/([^\/\.]+)(\/?|\.[^\/\.\?]*)(\?.*)?$/", $s, $m);
return ($m[1] !== null) ? (($returnExt) ? $m[1].$m[2] : $m[1]) : '';
}
$strArr = array(
'http://www.example.com/coolfolder/document.php?foo=bar',
'http://www.example.com/somefile.php',
'http://www.example.com/folder/?foo=bar',
'http://www.example.com/anotherFolder',
'http://www.example.com/',
'../rel/afile.php',
$_SERVER['REQUEST_URI']
);
foreach ($strArr as $str) {
$s = lastPartUrl($str);
$s2 = lastPartUrl($str, false);
if (strlen($s)) {
echo $s.' (with $returnExt passed as false:->) '.$s2.'<br>';
} else {
echo 'NO FILE OR FOLDER NAME FOUND<br>';
}
}
?>